我有一个名为film.csv的csv文件,每列的标题如下(有几行示例):
Year;Length;Title;Subject;Actor;Actress;Director;Popularity;Awards;*Image
1990;111;Tie Me Up! Tie Me Down!;Comedy;Banderas, Antonio;Abril, Victoria;Almodóvar, Pedro;68;No;NicholasCage.png
1991;113;High Heels;Comedy;Bosé, Miguel;Abril, Victoria;Almodóvar, Pedro;68;No;NicholasCage.png
1983;104;Dead Zone, The;Horror;Walken, Christopher;Adams, Brooke;Cronenberg, David;79;No;NicholasCage.png
1979;122;Cuba;Action;Connery, Sean;Adams, Brooke;Lester, Richard;6;No;seanConnery.png
1978;94;Days of Heaven;Drama;Gere, Richard;Adams, Brooke;Malick, Terrence;14;No;NicholasCage.png
1983;140;Octopussy;Action;Moore, Roger;Adams, Maud;Glen, John;68;No;NicholasCage.png
我需要用基本命令解析此csv(不使用Pandas)
1.我如何提取所有的电影标题与演员的名字=理查德,在1985年之前,和奖项=是吗?(我已经能够让它显示lisy在哪里奖项==是,但不是其余的)
1.如何计算某个演员在列表中出现的次数?
file_name = "film.csv"
print('loading file')
lines = (line for line in open(file_name,encoding='cp1252')) #generator to capture lines
print('removing ;')
lists = (s.rstrip().split(";") for s in lines) #generators to capture lists containing values from lines
print('2-filter by awards')
sel = input()
if sel == '2':
cols=next(lists) #obtains only the header
print(cols)
collections = (dict(zip(cols,data)) for data in lists)
filtered = (col["Title"] for col in collections if col["Awards"][0]== "Y")
for item in filtered:
print(item)
# input()
#browse lists and index them per header values, then filter all movies that have been awarded
#using a new generator object
else:
2条答案
按热度按时间drkbr07n1#
要读取和过滤数据,可以使用下一个示例(我使用
award == No
,因为示例中没有满足award == Yes
和其他条件的movie):印刷品:
要获得参与者的计数器,可以使用
collections.Counter
:印刷品:
unhi4e5o2#
这将打印出演员的名字是Richard、1985年之前拍摄的所有电影标题和奖项==是:
如果任何给定参与者出现在列表中,则进行计数:
输出:计数:1