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

Discussão em 'Python' iniciado por Stack, Fevereiro 7, 2025.

  1. Stack

    Stack Membro Participativo

    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.

    Continue reading...

Compartilhe esta Página