我已经测试了下面的代码,除了第 29 行之外,一切都按照我的预期进行。final.write(invrow) 实际上并没有写入文件。当我使用简单的打印 invrow 时,它显示没有问题。我没有错误,这里发生了什么?我错过了什么?我做错了什么?
import csv
inv = csv.DictReader(open('ireport.txt', 'rU'), dialect='excel-tab', delimiter="\t")
vbr = csv.DictReader(open('vb.txt', 'rU'), dialect='excel-tab', delimiter="\t")
mainr = csv.DictReader(open('main.txt', 'rU'), dialect='excel-tab', delimiter="\t")
final = open('ireport2.tab', 'w')
inva = []
maina = []
vba = []
for row in inv:
inva.append(row)
for row in mainr:
maina.append(row)
for row in vbr:
vba.append(row)
for invrow in inva:
for mainrow in maina:
try:
if invrow['\xef\xbb\xbfPART_CODE'] == mainrow['PART CODE']:
invrow['MAINQTY'] = mainrow['ON-HAND']
print invrow # works
final.write(invrow) #doesn't work. Why?
except TypeError:
pass
except ValueError:
pass
final.close()