如果文件未排序,Python会比较炸弹
我已经编写了一些代码来通过搜索字符串比较两个文件 文件=主数据文件 检查文件=州和地区列表 当我在文件中有超过1个未按顺序排序的状态时,它会爆炸 我如何在不必对“文件”进行排序的情况下使其工作 错误消息:回溯(最近一次调用last): 文件“/gangnamstyle.py”,第27行,在 csvLineList_2=csv2[lineCount]。拆分(“,”) 索引器:列表索引超出范围 我的代码:如果文件未排序,Python会比较炸弹,python,csv,Python,Csv,我已经编写了一些代码来通过搜索字符串比较两个文件 文件=主数据文件 检查文件=州和地区列表 当我在文件中有超过1个未按顺序排序的状态时,它会爆炸 我如何在不必对“文件”进行排序的情况下使其工作 错误消息:回溯(最近一次调用last): 文件“/gangnamstyle.py”,第27行,在 csvLineList_2=csv2[lineCount]。拆分(“,”) 索引器:列表索引超出范围 我的代码: #!/usr/bin/python import csv file = raw_input(
#!/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()
好的,因此错误消息是一个
索引器:在csvLineList_2=csv2[lineCount].split(“,”)
行中列出索引超出范围。这里只有一个索引,所以显然lineCount
对于csv2来说太大了
lineCount是范围(len(csv1))的值之一。这使得它自动在csv1的范围内。显然,csv1和csv2的长度不同,导致索引器错误
这是完全可能的,因为它们包含来自不同文件的行。显然,这些文件的行数并不相等
老实说,我根本不知道你为什么要把这些行读到csv1中。在这些行上循环并将其拆分(到变量csvLineList_1
),但从不使用该变量
我认为你的循环应该是:
for line in csv2:
parts = line.strip().split(",") # line.strip() removes whitespace and the newline
# at the end of the line
if search_string == parts[0]:
f1.write(parts[2] + "\n") # Add a newline, you probably want it
我希望这会有所帮助。您遇到的错误可能是由于文件长度不相等
从你所写的,你希望做的,都不清楚。在我看来(也许)你想在“主文件”中找到一个搜索词,如果你找到了,把你找到的那行写在“保存文件”中。在我看来,您还希望在“检查文件”的第一个字段中找到相同的搜索词,如果找到了,请将第三个字段的内容写入“保存文件”。如果这是错误的,那是因为你的代码有bug
无论哪种方式,您发布的代码中都存在一系列问题,并且您可能至少会从使用csv
模块来完成您想要做的事情中获得一些好处
可能会发布更完整的问题描述
编辑:
它“爆炸了”。错误消息是什么,发生在哪一行?这是错误:回溯(最近一次调用):csvLineList_2=csv2[lineCount]中的文件“/gangnamstyle.py”,第27行。拆分(“,”)索引器:列表索引超出范围感谢工作正常。。我只需要弄清楚如何让每一行都有这个项目。。。现在我得到了:DC,22,2109%,0.64 DC,44,4218%,12.1 DC,1231,12,12%,12 DC,131121,11%,11区域1我更希望DC,131121,11%,11区域1 DC,1231,12,12%,12区域1都在一个新的行上,当然,当我发布python的行限制是什么时,它的格式不正确。。一个文件有52个,一个文件有54个。Remcogerlich帮了大忙。带着他的回答。。文件分别为55行和52行。这只是在我扩展到更大文件之前的一个测试。我想做的是在“文件”检查文件中有状态、项目、人员、差异、天数。文件有状态、完整状态、名称、区域。我想做的是,当他们在“搜索字符串”(例如DC)中输入一个状态代码时,它会返回所有DC行,并在该行后面加上区域名称。所以在文件中你可能有DC,12345678,John,4%,5,在checkfile中你有所有的州,比如DC,哥伦比亚特区,Region1,所以我认为你有两个文件,你想在“State”上加入它们,并从一个文件输出所有内容,从另一个文件输出所有内容?我添加了一个完整的解决方案,可能满足你的需要。请就此提出问题。:)
import csv
import sys
def build_state_lookup(fn):
with open(fn) as infile:
reader = csv.reader(infile)
# throw away first line
reader.next()
# now build a dictionary mapping state to region
lookup = {state: region for (state, _, region) in reader}
return lookup
def process_big_file(in_fn, checkfile, out_fn):
lookup = build_state_lookup()
with open(in_fn) as infile:
with open(out_fn, 'w') as outfile:
reader = csv.reader(infile)
writer = csv.writer(outfile)
# output the header row
writer.writerow(reader.next() + ['Region'])
for row in reader:
state = row[0]
region = lookup.get(state, "No Region Found")
row.append(region)
writer.writerow(row)
def main():
process_big_file(*sys.argv[1:])
if __name__ == '__main__':
main()