Skip to content
Advertisement

How to remove extra quotes in between quotes for following example “Dec 01, 1999″,”Pocket Aquarium “Pocker” Pocket”,”Random : USA”,”USA” using python

I want to remove extra quotes in each line of csv file. ex:

 "Dec 01, 1999","Pocket Aquarium “Pocker" Pocket","Random : USA","USA"

ideal output required:

"Dec 01, 1999","Pocket Aquarium Pocker Pocket","Random : USA","USA"

Advertisement

Answer

you could try this:

input:

"Dec 01, 1999","Pocket Aquarium "Pocker" Pocket","Random : USA","USA"

code:

import csv

# reading the csv
with open('test.csv', "r+") as csvfile:
  # reading with csv object
    lines = csv.reader(csvfile, delimiter=',')

    modified_lines = []
    for line in lines:
        modified_lines.append([item.replace('"', "") for item in line])

    # creating a new csv with modified values
    with open("test_modified.csv", "w+") as csv_modified:
        for line in modified_lines:

            # formatting the lines
            line = ['"' + item + '"' for item in line]
            line = ",".join(line) + "n"

            # writing modified lines
            csv_modified.write(line)

test_modified.csv

"Dec 01, 1999","Pocket Aquarium Pocker Pocket","Random : USA","USA"
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement