简体   繁体   中英

Remove newline character after specific words in csv?

I have a big csv file. After some items there is a newline character which is not supposed to be there. It is always after a specific item, let's say it's called 'foo'. I need to remove every newline character after foo. I figured out this is kind of what should happen:

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

How do I make sure I output the result back to the file?

You can't write line back to your original file but assuming you will use your script like python script.py < input_file.csv > output_file.csv you can simply print the lines you need:

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='')

This answer just saves to a new csv file.

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) 

Tested on a test.csv file:

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

And gained a new.csv file:

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

I haven't tested this, but it should do what you need it to. This assumes there are no other items (other than foo) that has trailing white space that you don't want to strip. Otherwise, a simple conditional will fix that.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM