简体   繁体   中英

Replace double quote (") into single quote (') in CSV file using Python

I have CSV file containing below string as a field value for one column

It's Time. Say "I Do" in my Style.

So I want to convert double quote (") into single quote (')

expected output string: It's Time. Say 'I Do' in my Style.

I want to replace thing and save modified data in the same CSV file.

Can you I use regex using python?

Here is what you need:

with open('C:/path/to/csv', 'r') as f:  // open in read mode
    text = f.read()  // read and save the text of file in a variable

converted_text = text.replace('"', "'")  // replace characters, regex not required here, but this is where you will use it if required

with open('C:/path/to/csv', 'w') as f:  // open in write mode now
    f.write(converted_text)  // write the file. Old data in the file will be removed

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