尝试迭代csv文件时出现无提示错误(python)(Silent error when trying to iterate through a csv file (python))

编程入门 行业动态 更新时间:2024-10-23 03:26:48
尝试迭代csv文件时出现无提示错误(python)(Silent error when trying to iterate through a csv file (python))

我正在尝试制作一些脚本来使用源csv文件,将其与另一个文件进行比较,并使用第一个文件替换第二个文件中具有相同第一列的行。

我正在使用该代码:

#!/usr/bin/env python2 import csv import sys f = open(sys.argv[1], 'rt') t = open(sys.argv[2], 'wt') r = open(sys.argv[2], 'rt') try: patch = csv.reader(f) panorama = csv.reader(r) target = csv.writer(t) # row2 = 0 for row in patch: # print row[0] for row2 in panorama: print row print row[0], row2[0] if row[0] == row2[0]: target.writerow((row[0], row[1],row[2],row[3],row[4],row[5],row[6],row[7],row[8])) finally: f.close() t.close() r.close()

不幸的是,这失败没有任何错误,但我的调试打印不打印任何东西。 第一个评论的打印正常工作,并打印我的第一个文件的第一列。

你能帮我找一下这有什么问题吗?

谢谢你的帮助!

I'm trying to make some script to use a source csv file, compare it with another one and replace lines that have the same first column in the second file using the first one.

I'm using that code:

#!/usr/bin/env python2 import csv import sys f = open(sys.argv[1], 'rt') t = open(sys.argv[2], 'wt') r = open(sys.argv[2], 'rt') try: patch = csv.reader(f) panorama = csv.reader(r) target = csv.writer(t) # row2 = 0 for row in patch: # print row[0] for row2 in panorama: print row print row[0], row2[0] if row[0] == row2[0]: target.writerow((row[0], row[1],row[2],row[3],row[4],row[5],row[6],row[7],row[8])) finally: f.close() t.close() r.close()

Unfortunately, that fails without any errors, but my debug prints doesn't print anything. The first commented print is properly working and prints the first column of my first file.

Could you please help me to find what's wrong with this?

Thanks for your help!

最满意答案

您的方法存在两个问题。 第一个是打开相同的文件进行读/写(它可以在诸如fileinput之类的模块的帮助下完成,但由于没有csv支持,它会很混乱)。 第二个问题是你有嵌套for循环。 在第一行patch ,您已经用尽了panorama文件。 你需要的是两个非嵌套循环。 这是我的方法:

import csv import sys if __name__ == '__main__': with open(sys.argv[1], 'rt') as patch_file, \ open(sys.argv[2], 'rt') as pano_file, \ open(sys.argv[3], 'wt') as output_file: patch_lookup = {row[0]: row for row in csv.reader(patch_file)} pano = csv.reader(pano_file) output = csv.writer(output_file) for row in pano: row = patch_lookup.get(row[0], row) output.writerow(row)

在我的解决方案中, patch_lookup是一个字典,其中键是第一个字段,值是整行。 在阅读pano ,以下行将查找修补程序中的行并替换它(如果适用):

row = patch_lookup.get(row[0], row)

查找之后,我们将行写入输出。

There are two problems with your approach. The first is openning the same file for read/write (it can be done with help of module such as fileinput, but it will be messy because of no csv support). The second problem is you have nested for loop. After the first row of patch, you already exhausted the panorama file. What you need is two non-nested loops. Here is my approach:

import csv import sys if __name__ == '__main__': with open(sys.argv[1], 'rt') as patch_file, \ open(sys.argv[2], 'rt') as pano_file, \ open(sys.argv[3], 'wt') as output_file: patch_lookup = {row[0]: row for row in csv.reader(patch_file)} pano = csv.reader(pano_file) output = csv.writer(output_file) for row in pano: row = patch_lookup.get(row[0], row) output.writerow(row)

In my solution, patch_lookup is a dictionary where the key is the first field and value is the whole row. While reading the pano, the following line will look up the row in the patch and replace it if applicable:

row = patch_lookup.get(row[0], row)

After the look up, we will write the rows to the output.

更多推荐

本文发布于:2023-08-01 03:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1352878.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:提示   错误   迭代   文件   csv

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!