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

问题描述 投票:0回答:3

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

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

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

python python-3.x csv
3个回答
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='')

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)

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
© www.soinside.com 2019 - 2024. All rights reserved.