我读了两个这样的.csv文件。
ori = "all.csv"
det = "find.csv"
names = []
namesa = []
with open(det, "r") as cursor:
for row in cursor:
cells = row.split(",")
if len(cells) > 2:
b = cells[1]
c = b.split("-")
names.append(c[0])
with open(ori, "r") as rcursor1: #read the document
for trow in rcursor1: #read each row
row1 = trow.split(",") #split it by your seperator
namesa.append(row1)
工作得很好。
namesa
是一个嵌套列表,其中.csv中的每一行都是一个列表(请参阅示例),而names
包含我想在namesa
中找到的值。
如果names
中的值在namesa
中,我想要整个"嵌套列表部分"。那就是。
#example
namesa = [[a,b,c,], [a1, b1, c1], [xy, cd, e2], [u1, i1, il], ...]
names = [a, u1,]
return = [[a1, b1, c1], [u1, i1, il], ...]
#or
namesa = [[john,bill,catherina,], [marti, alex, christoph], [ben, sherlock, london], [Bern, paris, Zürich], ...]
names = [sherlock, marti]
results = [[marti, alex, christoph], [ben, sherlock, london]]
嗯,这不起作用。 那就是我到目前为止所尝试的内容:
#did not return any match
d = list([b for b in namesa if b in [a for a in names]])
print d
#did not return any match neither
for a in namesa:
for b in names:
if b in a:
print "match"
#well, that did not work neither
for a in namesa:
for b in names:
if a[5] == b:
print "match"
没有比赛回来。我在excel中打开了两个csv文件并用手搜索了#34;对于那些让我回复的比赛...... 我在这做错了什么?使用python。
答案 0 :(得分:1)
namesa = [['john', 'bill', 'catherina'], ['cat', 'dog', 'foo'], ['noodle', 'bob']]
names = ['john','foo']
试试这个
for n in names:
for arr in namesa:
if n.strip() in ''.join(arr):
print arr
.strip
因为names
列表中的值似乎有尾随空格。
答案 1 :(得分:1)
如果您使用.csv文件,我建议您使用csv模块。
我这样做(我假设你要找的东西是'姓'栏。如果它们在不同的栏目中,你可以考虑用它们进行迭代,或者做name in row['surname'] or name in row['name']
,取决于并发症:
import csv
result = []
listFromCSV = []
names = ['alex','sherlock']
csvFile = open('yourFile.csv')
reader = csv.DictReader(csvFile)
fieldnames = reader.fieldnames
for row in reader:
listFromCSV.append(row)
csvFile.close()
for name in names:
for row in listFromCSV:
if name.strip() in row['surname']:
result.append(row)
如果你想摆脱重复,可以在最后一个句点附加break
for循环。
答案 2 :(得分:0)
namesa = [['john','bill','catherina',], ['marti', 'alex', 'christoph'], ['ben', 'sherlock', 'london']]
names = ['sherlock', 'marti']
for i in namesa:
for j in names:
if j in i:
print i
输出
['marti','alex','christoph'] ['ben','sherlock','london']