有人可以幫助我解決這個嵌套循環嗎?它具有與Loops not working - Strings (Python)相同的問題,但現在它在csv類中沒有csv.readline()函數。再一次,如何讓嵌套循環在python中工作
import csv
import sys, re
import codecs
reload(sys)
sys.setdefaultencoding('utf-8')
reader = csv.reader(open("reference.txt"), delimiter = "\t")
reader2 = csv.reader(open("current.txt"), delimiter = "\t")
for line in reader:
for line2 in reader2:
if line[0] == line2[1]:
print line2[0] + '\t' + line[0]
print line[1]
else:
print line[0]
print line[1]
此代碼的目的是檢查在與當前文本文件(即閱讀器)一致的參考文本(即reader2)的行。然後打印序列號,也就是在reference.txt
reference.txt看起來像這樣(的序列號之間的空間和句子是tab
):
S00001LP this is a nested problem
S00002LP that cannot be solved
S00003LP and it's pissing me off
S00004LP badly
current.txt樣子本(第1和第2句之間的空間是):
this is a nested problem wakaraa pii ney bay tam
and i really can't solve it shuu ipp faa luiip
so i come to seek help from stackoverflow lakjsd sdiiije
seriously it is crazy because such foo bar bar foo
problems don't happen in other languages whaloemver ahjd
and it's pissing me off gaga ooo mama
badly wahahahah
所需的輸出會是這個樣子:
S00001LP this is a nested problem wakaraa pii ney bay tam
and i really can't solve it shuu ipp faa luiip
so i come to seek help from stackoverflow lakjsd sdiiije
seriously it is crazy because such foo bar bar foo
problems don't happen in other languages whaloemver ahjd
S00003LP and it's pissing me off gaga ooo mama
S00004LP badly wahahahah
'不工作'是沒有錯誤的描述。 – Bobby
[Select is not broken](http://lingpipe-blog.com/2007/06/27/select-isnt-broken-or-horses-not-zebras/) - Python的循環工作得很好。 –