【发布时间】:2013-12-10 20:39:48
【问题描述】:
我编写了一些代码来通过搜索字符串比较两个文件。
文件 = 主数据文件 检查文件 = 州和地区列表
当我在文件中有超过 1 个未按排序顺序排列的状态时,它会爆炸。
我怎样才能让它工作而不必对我的“文件”进行排序
错误消息:回溯(最近一次调用最后一次): 文件“./gangnamstyle.py”,第 27 行,在 csvLineList_2 = csv2[lineCount].split(",") IndexError: 列表索引超出范围
我的代码:
#!/usr/bin/python
import csv
file = raw_input("Please enter the file name to search: ") #File name
checkfile = raw_input("Please enter the file with the search data: ") #datafile
save_file = raw_input("Please enter the file name to save: ") #Save Name
search_string = raw_input("Please type string to search for: ") #search string
#row = raw_input("Please enter column text is in: ") #column number - starts at 0
#ID_INDEX = row
#ID_INDEX = int(ID_INDEX)
f = open(file)
f1 = open(save_file, 'a')
csv1 = open(file, "r").readlines()
csv2 = open(checkfile, "r").readlines()
#what looks for the string in the file
copyline=False
for line in f.readlines():
if search_string in line:
copyline=True
if copyline:
f1.write(line)
for lineCount in range( len( csv1) ):
csvLineList_1 = csv1[lineCount].split(",")
csvLineList_2 = csv2[lineCount].split(",")
if search_string == csvLineList_2[0]:
f1.write(csvLineList_2[2])
f1.close() #close saved file
f.close() #close source file
#csv1.close()
#csv2.close()
【问题讨论】:
-
它“爆炸了”。错误信息是什么,发生在哪一行?
-
这是错误:回溯(最近一次调用最后一次):文件“./gangnamstyle.py”,第 27 行,在
csvLineList_2 = csv2[lineCount].split(",") IndexError: 列表索引超出范围