我试图解决下面的实验和有问题.这个问题涉及csv输入.有解决方案需要满足的标准.任何帮助或提示都将不胜感激.我的代码是在问题的末尾沿着我的输出.
Each row contains the title, rating, and all showtimes of a unique movie.
A space is placed before and after each vertical separator ('|') in each row.
Column 1 displays the movie titles and is left justified with a minimum of 44 characters.
If the movie title has more than 44 characters, output the first 44 characters only.
Column 2 displays the movie ratings and is right justified with a minimum of 5 characters.
Column 3 displays all the showtimes of the same movie, separated by a space.
这是输入:
16:40,Wonders of the World,G
20:00,Wonders of the World,G
19:00,End of the Universe,NC-17
12:45,Buffalo Bill And The Indians or Sitting Bull's History Lesson,PG
15:00,Buffalo Bill And The Indians or Sitting Bull's History Lesson,PG
19:30,Buffalo Bill And The Indians or Sitting Bull's History Lesson,PG
10:00,Adventure of Lewis and Clark,PG-13
14:30,Adventure of Lewis and Clark,PG-13
19:00,Halloween,R
以下是预期输出:
Wonders of the World | G | 16:40 20:00
End of the Universe | NC-17 | 19:00
Buffalo Bill And The Indians or Sitting Bull | PG | 12:45 15:00 19:30
Adventure of Lewis and Clark | PG-13 | 10:00 14:30
Halloween | R | 19:00
我的代码到目前为止:
import csv
rawMovies = input()
repeatList = []
with open(rawMovies, 'r') as movies:
moviesList = csv.reader(movies)
for movie in moviesList:
time = movie[0]
#print(time)
show = movie[1]
if len(show) > 45:
show = show[0:44]
#print(show)
rating = movie[2]
#print(rating)
print('{0: <44} | {1: <6} | {2}'.format(show, rating, time))
我的输出没有右对齐评级,我不知道如何过滤重复的电影而不删除列表的时间部分:
Wonders of the World | G | 16:40
Wonders of the World | G | 20:00
End of the Universe | NC-17 | 19:00
Buffalo Bill And The Indians or Sitting Bull | PG | 12:45
Buffalo Bill And The Indians or Sitting Bull | PG | 15:00
Buffalo Bill And The Indians or Sitting Bull | PG | 19:30
Adventure of Lewis and Clark | PG-13 | 10:00
Adventure of Lewis and Clark | PG-13 | 14:30
Halloween | R | 19:00
5条答案
按热度按时间wqlqzqxt1#
您可以在字典中收集输入数据,以title-rating-tuples作为键,并在列表中收集放映时间,然后打印合并的信息。例如(您必须调整文件名):
输出:
由于输入看起来像是在连接的块中出现的,所以您也可以使用
itertools.groupby
(来自标准库),并在阅读时打印:i7uaboj42#
为此,考虑评级字符串的最大长度。从该值中减去评级的长度。制作该长度的空格字符串并附加评级。所以基本上
也只是替换
f字符串,更容易阅读
ehxuflar3#
下面是我在社区的一些提示后得出的结论。
unftdfkk4#
我将使用Python的
groupby()
函数来实现这一点,它可以帮助您将具有相同值的连续行分组。例如:
为您提供:
groupby()
如何工作?当阅读CSV文件时,您将一次获取一行。
groupby()
所做的是将行分组到包含具有相同值的行的迷你列表中。它查找的值使用key
参数给出。在这种情况下,lambda函数一次传递一行,它返回x[1]
的当前值,即title
。groupby()
会一直阅读行,直到该值发生变化。然后,它将当前列表作为entries
作为迭代器返回。这种方法确实假设你想要分组的行是文件中连续的行。你甚至可以编写你自己的group by generator函数:
kiayqfof5#