打印相應的行我有這樣一個CSV文件:(無頭)搜索一個文件的行中的其他文件和蟒蛇
aaa,1,2,3,4,5
bbb,2,3,4,5,6
ccc,3,5,7,8,5
ddd,4,6,5,8,9
我想搜索另一個csv文件:(無頭)
bbb,1,2,3,4,5,,6,4,7
kkk,2,3,4,5,6,5,4,5,6
ccc,3,4,5,6,8,9,6,9,6
aaa,1,2,3,4,6,6,4,6,4
sss,1,2,3,4,5,3,5,3,5
並打印第一個文件中存在的第二個文件中的行(基於第一列的匹配)。因此,結果將是:
bbb,1,2,3,4,5,,6,4,7
ccc,3,4,5,6,8,9,6,9,6
aaa,1,2,3,4,6,6,4,6,4
我有下面的代碼,但它不顯示任何信息:
labels = []
with open("csv1.csv", "r") as f:
f.readline()
for line in f:
labels.append((line.strip("\n")))
with open("csv2.csv", "r") as f:
f.readline()
for line in f:
if (line.split(",")[1]) in labels:
print (line)
如果可能的話,你能告訴我如何做到這一點,好嗎?我的代碼有什麼問題?提前致謝 !
你有'熊貓'嗎? –