0

I have a large batch of CSVs containing numeric and string columns, in which there will occasionally appear the combination of two special characters \". This combination will occasionally appear at the end of a string, such as "string\"", and the adjacent double quotes can confuse some software when attempting to import. I want to strip all CSVs of \" (but obviously not of individual " or \ characters) and then save/replace the CSVs. How can I do it? Python or R preferred.

David Smerdon
  • 59
  • 1
  • 6
  • https://stackoverflow.com/questions/39983633/how-to-read-csv-with-sequence-inside-quoted-character-value-in-r – crestor Aug 11 '21 at 12:56

1 Answers1

1

Using input file

slash_quote.csv:

"a","string\"","b"

with code:

files = ("slash_quote.csv",)

for filename in files:
    with open(filename, "r") as f:
        with open(filename+"_new", "w") as outfile:
            outfile.write(f.read().replace(r'\"', ''))
            # if replacement is desired include
            os.rename(f"{filename}_new", filename)

output file:

"a","string","b"
ChrisFreeman
  • 5,831
  • 4
  • 20
  • 32