1

我需要从具有特定模式的 csv 中删除一行

这就是我的 csv 文件的外观。

lbm,16730,0
namd,16733,6
namd,16731,2
namd,16732,4

如果我想删除带有模式的行16730并按原样输出文件的其余部分..

所以,输出是这样的:

namd,16733,6
namd,16731,2
namd,16732,4

我怎么做?

这是我在互联网上的一些文件的帮助下编写的一个小脚本

def delete_line(dello):
    opener = open(input_csv, 'rb')
    dataset = csv.reader(opener, delimiter=',')
    output = []
    for line in dataset:
            if 'dello' == line[1]:
                    print line[1]
                    #i dont know how to strip it here
                    output.append(line)
    opener.close()
    fn = input_csv
    f = open(fn,'w')
    f.writelines(output)
    f.close()

任何提示我哪里出错了?

4

4 回答 4

1

如果您选择遵循 uʍop ǝpısdn 的建议并使用 grep,这将起作用:

grep -v ",16370," path/to/file > path/to/new_file

假设文件的结构类似于示例中的结构并且在所有文件中都是一致的,但是......

仅供参考,在 grep 中,-v 表示反向匹配,返回所有包含指定模式的结果。

EDIT: If you need to preserve the original file, you can use a temporary and then restore its name to the original one:

grep -v ",16370," path/to/file > path/to/new_file && rm path/to/file && mv path/to/new_file path/to/file
于 2013-02-05T18:47:01.130 回答
1

If you need python, then use this:

def delete_line(dello):
    data = open("abc.csv").readlines()

    i = 0
    for line in data:
        if dello in line:
            data.pop(i)
        i += 1

    open("abc.csv", "w").write("".join(data))

delete_line("16732")

Input:

lbm,16730,0
namd,16733,6
namd,16731,2
namd,16732,4

Output:

lbm,16730,0
namd,16733,6
namd,16731,2

Note: this will remove all the entries matching the string.


Update

Modifying your code:

import csv

def delete_line(dello):
    opener = open("abc.csv", 'rb')
    dataset = csv.reader(opener, delimiter=',')
    output = []
    for line in dataset:
        # Add to output only if not matching the string
        if dello != line[1]:
            # Need join as line is a list
            output.append(",".join(line) + "\n")
    opener.close()

    fn = "abc.csv"
    f = open(fn,'w')
    f.writelines(output)
    f.close()

delete_line("16730")

If you need to strip out an entry, you can use dataset.pop(index).

于 2013-02-05T18:59:20.343 回答
0

there are two ways to run a python script like that:

first: adding a main section to the file like:

if __name__ == "__main__":
    delete_line(some_paramter)

and then running from the command line:

python scriptfilename.py

or from python shell:

from scriptfilename import delete_line
delete_line(some_parameter)

do you use one of those? a script doesn't run itself.

some more unclarified business with your script:

  1. what exactly is the input variable dello used in your script. there is a line using the string 'dello' but not the parameter dello. what did you try to do here

  2. are you running it in the same folder with the input_csv file. is input_csv the full file name or is it input_csv.csv.

  3. you are using input_csv as a variable, which is empty and would fail, if this isn't a variable holding the file name (from some place else in the file) you should call the file name as a string: 'input_csv.csv'

  4. you commented: #i dont know how to strip it here? what exactly is the question and what do you mean by that? using strip(). something else?

  5. did your script import csv before the function? if not.. nothing would work.

  6. It is always more recommanded to work with with when working with files. handles exceptions and automatic file closing. you can read about that in numerous places.

something like:

with open('file.csv','rb') as f:
    dataset = csv.reader(f)
    #the rest. and you don't need closing etc..'
于 2013-02-05T19:01:25.790 回答
0

The first problem:

if 'dello' == line[1]:
  1. You refer to dello with quote
  2. You have the logic reverse.

Therefore, the correct test should be:

if dello != line[1]:

The second problem: you read the file as a CSV, but write it out as an ordinary file. You should be consistent by either read and write as an ordinary text file, or as CSV. Mixing them up makes it hard to get the right output.

The third is not really a problem, but a suggestion: don't hard code the file name, pass it into your function. That way, your function is more versatile.

Here is my proposed code:

def delete_line(input_csv, dello):
    with open(input_csv, 'rb') as f:
        csv_reader = csv.reader(f)
        output = []
        for line in csv_reader:
            if dello not in line:
                output.append(line)

    with open(input_csv, 'wb') as f:
        csv_writer = csv.writer(f)
        csv_writer.writerows(output)
于 2013-02-05T19:21:36.547 回答