pythoncsv

Using Python to replace triple double quotes with single double quote in CSV


I used the pandas library to manipulate the original data down to a simple 2 column csv file and rename it to a text file. The file has triple double quotes that I need replaced with single double quotes. Every line of the file is formatted as:

"""QA12345""","""Some Other Text"""

What I need is:

"QA12345","Some Other Text"

This python snippet wipes out the file after it finishes.

with fileinput.FileInput(input_file, inplace=True) as f:
    next(f)
    for line in f:
        line.replace("""""", '"')

It doesn't work with

line.replace('"""', '"') 

either.

I've also tried adjusting the input values to be '"Some Other Text"' and variations (""" and '\"') but nothing seems to work. I believe the triple quote is causing the issue, but I don't know what I have to do to.


Solution

  • Judging by the OP's code, I guess the aim are

    1. Edit file in-place
    2. keep the first line unmodified
    3. For the rest of the lines, replace 3 double quotes with a single one

    My solution is almost the same, but with print():

    with fileinput.input(input_file, inplace=True) as stream:
        print(next(stream), end="")
        for line in stream:
            print(line.replace('"""', '"'), end="")
    

    That should give the desired result.