使用 Python 删除 CSV 文件中的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29725932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:56:08  来源:igfitidea点击:

Deleting rows with Python in a CSV file

pythoncsv

提问by Will B

All I would like to do is delete a row if it has a value of '0' in the third column. An example of the data would be something like:

我想要做的就是删除第三列中的值为“0”的行。数据的一个例子是这样的:

6.5, 5.4, 0, 320
6.5, 5.4, 1, 320

So the first row would need to be deleted whereas the second would stay.

所以第一行需要删除,而第二行会保留。

What I have so far is as follows:

到目前为止,我所拥有的如下:

import csv
input = open('first.csv', 'rb')
output = open('first_edit.csv', 'wb')
writer = csv.writer(output)
for row in csv.reader(input):
    if row[2]!=0:
        writer.writerow(row)
input.close()
output.close()

Any help would be great

任何帮助都会很棒

采纳答案by u1860929

You are very close; currently you compare the row[2]with integer 0, make the comparison with the string "0". When you read the data from a file, it is a string and not an integer, so that is why your integer check fails currently:

你很亲近;当前您将row[2]与 integer0进行比较,与 string 进行比较"0"。当您从文件中读取数据时,它是一个字符串而不是整数,这就是您的整数检查当前失败的原因:

row[2]!="0":

Also, you can use the withkeyword to make the current code slightly more pythonic so that the lines in your code are reduced and you can omit the .closestatements:

此外,您可以使用with关键字使当前代码稍微更pythonic,以便减少代码中的行数并且您可以省略.close语句:

import csv
with open('first.csv', 'rb') as inp, open('first_edit.csv', 'wb') as out:
    writer = csv.writer(out)
    for row in csv.reader(inp):
        if row[2] != "0":
            writer.writerow(row)

Note that inputis a Python builtin, so I've used another variable name instead.

请注意,这input是一个 Python 内置函数,因此我使用了另一个变量名。



Edit: The values in your csv file's rows are comma andspace separated; In a normal csv, they would be simply comma separated and a check against "0"would work, so you can either use strip(row[2]) != 0, or check against " 0".

编辑:csv 文件行中的值以逗号空格分隔;在普通的 csv 中,它们只是用逗号分隔,并且可以进行检查"0",因此您可以使用strip(row[2]) != 0, 或检查" 0".

The better solution would be to correct the csv format, but in case you want to persist with the current one, the following will work with your given csv file format:

更好的解决方案是更正 csv 格式,但如果您想保留当前的格式,以下内容将适用于您给定的 csv 文件格式:

$ cat test.py 
import csv
with open('first.csv', 'rb') as inp, open('first_edit.csv', 'wb') as out:
    writer = csv.writer(out)
    for row in csv.reader(inp):
        if row[2] != " 0":
            writer.writerow(row)
$ cat first.csv 
6.5, 5.4, 0, 320
6.5, 5.4, 1, 320
$ python test.py 
$ cat first_edit.csv 
6.5, 5.4, 1, 320

回答by The Obscure Question

You should have if row[2] != "0". Otherwise it's not checking to see if the string value is equal to 0.

你应该有if row[2] != "0"。否则它不会检查字符串值是否等于 0。