gpt4 book ai didi

python - 使用python,如果该行中存在特定字符,如何删除csv文件中的整行?

转载 作者:行者123 更新时间:2023-11-30 23:46:57 24 4
gpt4 key购买 nike

我有一个很大的 csv 文件,有 216961 行:

9808,54,43,59,999,17,10,59,999,-1,0,0
9809,54,43,59,999,17,12,0,-1,0,0
9810,54,43,59,999,17,13,0,001,-1,0,0
9811,54,43,59,999,17,13,59,999,-1,0,0
9812,54,43,59,999,17,15,0,-1,0,0
9813,54,43,59,999,17,16,0,001,0,0,0
9814,54,43,59,999,17,16,59,999,0,0,0
9815,54,43,59,999,17,18,0,0,0,0
9816,54,43,59,999,17,19,0,001,0,0,0
9817,54,43,59,999,17,19,59,999,0,0,0
9818,54,43,59,999,17,21,0,0,0,0
9819,54,43,59,999,17,22,0,001,0,0,0
9820,54,43,59,999,17,22,59,999,0,0,0

我需要筛选文件并删除所有存在 -1 的行,大约 6900 行,所以我想请求你的帮助

我的尝试:

import csv
reader = csv.reader(open("file.csv", "rb"), delimiter=',')
for line in reader:
match = line[7]
if match not in '-1':
print line


但-1并不总是在第[7]行

最佳答案

尝试这种方法。

import csv
reader = csv.reader(open("file.csv", "rb"), delimiter=',')
for line in reader:
if "-1" not in line:
print line

关于python - 使用python,如果该行中存在特定字符,如何删除csv文件中的整行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8566342/

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