【发布时间】:2020-07-14 15:38:44
【问题描述】:
我的代码:
import csv
with open("firstfile.csv", encoding='utf-8-sig') as file1:
one = csv.DictReader(file1)
with open("secondfile.csv", "r") as file2:
for line in one:
print(line)
for line2 in file2:
s = line["Owner"]
if s in line2:
print(True)
break
print(s)
当我运行这段代码时,我得到了
{'File Name': 'hofie.exe', 'Owner': 'hello'}
hello
{'File Name': 'feiofejp.zip', 'Owner': 'yo'}
hello
{'File Name': 'fewfew1.exe', 'Owner': 'foooffoo'}
hello
当我期待时:
{'File Name': 'hofie.exe', 'Owner': 'hello'}
hello
{'File Name': 'feiofejp.zip', 'Owner': 'yo'}
yo
{'File Name': 'fewfew1.exe', 'Owner': 'foooffoo'}
foooffoo
firstfile.csv:
File Name,Owner
hofie.exe,hello
feiofejp.zip,yo
fewfew1.exe,foooffoo
secondfile.csv:
ihfoiehofiejwifpewhf
有什么问题?
【问题讨论】:
-
这应该回答了你的问题,因为
for line2 in file2在firstfile的第一行之后已经用尽了:iterating over file object in Python does not work, but readlines() does but is inefficient -
这很有趣。我从来没有意识到,但非常感谢你的帮助!当我阅读您在此处链接的帖子时,它说您可以搜索第一行,您能否快速显示我将把该行放在哪里?