Ich lese zwei csv-Dateien und drucke spezifische Spalte nach Schlüsselname. Zuerst habe ich eine Liste meiner Schlüsselnamen wie key = [a,b,c]
Python: Warum Whiscode nur Schleife einmal?
und ich diesen Code ein:
with open('./file/report.csv', 'rb') as csvfile,open('./file/all.csv','rb') as csvfile2:
reader2 = csv.DictReader(csvfile)
reader3 = csv.DictReader(csvfile2)
for i in key:
for row in reader2:
for row2 in reader3:
if row['Case Id'] == i and row2['name'] == i:
a=row['Status']
b = row2['result']
print a,b
zwei CSV-Dateien:
report.csv: all.csv:
Case Id Status name result
a 111 a 1111
b 222 b 2222
c 333 c 3333
Ergebnis meiner erwartet wird, wird es Schleife dreimal weil es drei Elemente gibt in key
list.exected Ergebnis sollte wie folgt aussehen:
111 1111
222 2222
333 3333
Aber tatsächliche Ergebnis ist:
111 1111
es nur Schleife einmal. Ich bin neu im Codieren von Dingen, brauche Hilfe! Vielen Dank!!
Danke! Es klappt! – shuoqi