简体   繁体   中英

Remove double quotes from CSV file

I would like to clean a CSV file which has in the middle of a string double quotes by removing all quotation marks inside the csv.
as an example: Ana are "mere". I would like that the quotes are removed so that I will have Ana are mere

I am trying to replace the " with a space but I cannot w and ra csv at the same time. How can I clean the data?

import csv
with open('/var/lib/neo4j/import/MRCONSO.csv',"r+",encoding="utf-8") as csv_file:
    for data in csv_file:
        data = [d.replace('"', '') for d in data]

I expect that the CSV structure will not change only the quotes will be removed.

you don't have to iterate lines, you can just read the full content, use replace as you did, and write the new content:

with open('/var/lib/neo4j/import/MRCONSO.csv', "r+", encoding="utf-8") as csv_file:
    content = csv_file.read()

with open('/var/lib/neo4j/import/MRCONSO.csv', "w+", encoding="utf-8") as csv_file:
    csv_file.write(content.replace('"', ''))

You can use pandas:

import pandas

df = pandas.read_csv('file.csv',header=None)
df.replace('"', '', inplace=True, regex=True)
df.to_csv("file.csv",header=False, index=False)

When using quotes " as strings, you have to pass a backslash before it or declare it as literal, else python would consider your entry as syntax for declaring a string:

df.replace('\"', '', inplace = True, regex = True)

or

data = [d.replace('\"', '') for d in data]

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