在csv中的特定单词后删除换行符?

时间:2017-12-22 16:24:11

标签: python python-3.x csv

我有一个很大的csv文件。在一些项目之后有一个换行符,不应该在那里。它总是在特定项目之后,假设它被称为'foo'。我需要在foo之后删除每个换行符。我发现这是应该发生的事情:

for line in sys.stdin:
    if line.split(",")[-1] == "foo":
        line = line.rstrip()

如何确保将结果输出回文件?

3 个答案:

答案 0 :(得分:3)

您无法将行写回原始文件,但假设您将使用python script.py < input_file.csv > output_file.csv之类的脚本,则只需print您需要的行:

import sys

for line in sys.stdin:
    if line.split(",")[-1] == "foo":
        line = line.rstrip()
    # print() will append '\n' by default - we prevent it
    print(line, end='')

答案 1 :(得分:0)

这个答案只保存到一个新的csv文件。

with open("test.csv", "r", newline="") as csvfile:
    my_reader = csv.reader(csvfile, delimiter=',', quotechar='"')
    with open("new.csv", "w", newline="") as csvfile2:
        last_line = []
        writer = csv.writer(csvfile2, delimiter=',', quotechar='"')
        for line in my_reader:
            if last_line != []:
                writer.writerow(last_line + line)
                last_line = []   
            elif line[-1] == "foo":
                last_line = line
            else:
                writer.writerow(line)
        if last_line != []:  # when the last line also contain "foo"
            writer.writerow(last_line) 

在test.csv文件上测试:

this,"is,a ",book
this,is,foo
oh,my
this,foo

获得了一个new.csv文件:

this,"is,a ",book
this,is,foo,oh,my
this,foo

答案 2 :(得分:0)

我还没有测试过这个,但它应该做你需要的。这假设没有其他项目(foo除外)具有您不想剥离的尾随空白区域。否则,一个简单的条件将解决这个问题。

import csv

with open("/path/to/file", newline='') as f:
    reader = csv.reader(f)

for row in reader:
    for i, item in enumerate(row):
        row[i] = item.rstrip()

with open("/path/to/file", 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerows(reader)