csv 如何按多个条件筛选集合

6yoyoihd  于 2022-12-06  发布在  其他
关注(0)|答案(1)|浏览(187)

我有一个名为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

我正在尝试筛选,并需要显示移动标题,符合此条件:名字包含“Richard”,年份〈1985,奖项==“Y”
我能过滤奖项,但不能其余的。你能帮忙吗?

file_name = "film.csv"
lines = (line for line in open(file_name,encoding='cp1252')) #generator to capture lines
lists = (s.rstrip().split(";") for s in lines) #generators to capture lists containing values from lines

#browse lists and index them per header values, then filter all movies that have been awarded
#using a new generator object

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()

这适用于奖项,但我不知道如何添加额外的过滤器。此外,当我试图过滤if col["Year"] < 1985时,我得到错误消息,因为string与int不兼容。我如何使年份成为一个值?我相信对于名字,我可以这样过滤:if col["Actor"].split(", ")[-1] == "Richard"

z3yyvxxp

z3yyvxxp1#

您知道如何添加一个筛选器。没有“附加”筛选器这样的东西。只需将您的条件添加到当前条件中即可。由于您希望 * 所有 * 条件都为True以选择记录,因此应使用布尔逻辑 * 和 *。例如:

filtered = (
             col["Title"] 
             for col in collections 
             if col["Awards"][0] == "Y"
            and col["Actor"].split(", ")[-1] == "Richard"
            and int(col["Year"]) < 1985
           )

请注意,我在col["Year"]周围添加了一个int(),以将其转换为整数。
您实际上已经在设置中重新设计了csv.DictReader来解决这个问题!

file_name = "film.csv"
lines = (line for line in open(file_name,encoding='cp1252')) #generator to capture lines
lists = (s.rstrip().split(";") for s in lines) #generators to capture lists containing values from lines

#browse lists and index them per header values, then filter all movies that have been awarded
#using a new generator object

cols=next(lists) #obtains only the header
print(cols)
collections = (dict(zip(cols,data)) for data in lists)
filtered = ...

您可以只执行以下操作:

import csv

file_name = "film.csv"
with open(file_name) as f:
    collections = csv.DictReader(delimiter=";")
    filtered = ...

相关问题