我正在尝试匹配两个文件之间的数据。
文件1:
# number of records, name
1234, keyword
文件2:
# date/time, name
2016-10-13| here is keyword in the name
因此,我希望将文件 3 写为:
# number of records, name, date
1234, here is keyword in the name, 2016-10-13
所以这里的想法是迭代文件 1,获取所有关键字并检查文件 2 是否存在。如果是这样,请从文件 2 中取出第一个字段(日期)并将其作为文件 1 中的最后一项。我编写了以下代码来测试一些内容,但效果不佳。我遇到的第一个问题是 python 在文件 2 中找不到任何关键字
。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import csv
FILE1='file1'
FILE2='file2'
file2data=[]
with open(FILE2, 'rb') as file2file:
reader = csv.reader(file2file, delimiter='|', quotechar='"')
for row in reader:
file2data.append(row)
def check(name):
print('checking: "%s"' % name)
rval=[]
for item in file2data:
if name in item:
rval.append(item)
return rval
with open(FILE1, 'rb') as csvfile:
csvreader = csv.reader(csvfile, delimiter=',', quotechar='"')
for row in csvreader:
entries=row[0]
keyword=row[1].strip()
checked=check(keyword)
if checked:
print('ok')
有谁知道为什么会这样吗?为什么在 check()
函数内部有以下代码
if name in item:
没有找到任何值?
最佳答案
这个
if name in item:
检查item
行(单元格列表)中是否存在具有确切内容name
的项目cell(item
实际上是您之前存储的行,命名错误:))
您需要扫描每个项目以查看是否包含该字符串。所以写:
if any(name in cell for cell in item):
相反
一旦在item
的单元格中找到 name
子字符串,any
将立即返回 True
。
请注意,这是子字符串匹配,而不是单词匹配。 key
将匹配包含keyword
的字符串。如果您想要单词匹配(通过根据空格分割单词):
if any(name in cell.split() for cell in item):
关于python - 两个文件之间的配对数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40022967/