gpt4 book ai didi

Python for 循环在迭代 CSV 行时不合理地中途停止

转载 作者:太空宇宙 更新时间:2023-11-03 15:59:52 25 4
gpt4 key购买 nike

我正在处理一个CSV文件来分析讲座反馈数据,格式如下

"5631","18650","10",,,"2015-09-18 09:35:11"
"18650","null","10",,,"2015-09-18 09:37:12"
"18650","5631","10",,,"2015-09-18 09:37:19"
"58649","null","6",,,"2015-09-18 09:38:13"
"45379","31541","10","its friday","nothing yet keep it up","2015-09-18 09:39:46"

我正在尝试删除不良数据。只有具有“id1”、“id2”另一个对应的“id2”、“id1”的数据条目才被视为有效。

我正在使用嵌套循环来尝试为每行查找匹配的条目。然而,外循环似乎无缘无故地中途停止了。这是我的代码

class Filter:
file1 = open('EncodedPeerInteractions.FA2015.csv')
peerinter = csv.reader(file1,delimiter=',')
def __init__(self):
super()

def filter(self):
file2 = open('FilteredInteractions.csv','a')
for row in self.peerinter:
print(row)
if row[0] == 'null' or row[1] == 'null':
continue
id1 = int(row[0])
id2 = int(row[1])
for test in self.peerinter:
if test[0] == 'null' or test[1] == 'null':
continue
if int(test[0]) == id2 and int(test[1]) == id1:
file2.write("\n")
file2.write(str(row))
break
file2.close()

我尝试使用 pdb 单步执行代码,前几个循环一切正常,然后突然跳转到 file2.close() 并返回。该程序确实打印出了一些有效的条目,但这还不够。

我测试了 csv 文件并将其正确加载到内存中,其中包含超过 18000 个条目。我使用 print 进行了测试,但它给出了相同的结果,因此附加文件没有任何问题。

编辑

现在我明白问题所在了。如this question说,当有匹配时我会中断,但当没有匹配时,内部循环将消耗所有文件而不重置它。当它返回到外循环时,它就结束了。我应该将其放入列表或让它重置。

最佳答案

你让这种方式变得更加复杂。

给定:

$ cat /tmp/so.csv
"5631","18650","10",,,"2015-09-18 09:35:11"
"18650","null","10",,,"2015-09-18 09:37:12"
"18650","5631","10",,,"2015-09-18 09:37:19"
"58649","null","6",,,"2015-09-18 09:38:13"
"45379","31541","10","its friday","nothing yet keep it up","2015-09-18 09:39:46"

您可以使用 csv 和过滤器来获取您想要的内容:

>>> with open('/tmp/so.csv') as f:
... list(filter(lambda row: 'null' not in row[0:2], csv.reader(f)))
...
[['5631', '18650', '10', '', '', '2015-09-18 09:35:11'],
['18650', '5631', '10', '', '', '2015-09-18 09:37:19'],
['45379', '31541', '10', 'its friday', 'nothing yet keep it up', '2015-09-18 09:39:46']]

关于Python for 循环在迭代 CSV 行时不合理地中途停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40440454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com