我正在编写一个代码,以读取csv文件并从列表中找到值,然后使用“ ABCD”对其进行更新。 我尝试使用编解码器读取csv,但没有运气。
import csv
import codecs
Testnumber = ['xxxxxxxxx','yyyyyyy']
i =0
is_in_file = "BLACK LISTED NUMBER NOT FOUND IN THIS BASE"
with open('D1_SD_ROI.csv', 'rb') as csvfile:
my_content = csv.reader(csvfile, delimiter=' ')
for row in my_content:
for A in row:
i=i+1 #couting number of loops eventually row number
for B in Testnumber:
if A == B:
print B
print i
r = csv.reader(csvfile)
lines = list(r)
lines[i-1][0] = 'ABCD'
writer = csv.writer(open('D1_SD_ROI.csv', 'w'))
writer.writerows(lines)
print ("D1_SD_ROI")
print is_in_file
我遇到此错误:
回溯(最近一次通话最后一次):文件“ sun.py”,第53行,在 lines [i-1] [0] ='ABCD'IndexError:列表索引超出 范围