Pradeep Bhutare
Pradeep Bhutare

Reputation: 45

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?

Upvotes: 1

Views: 2261

Answers (1)

Vaibhav Vishal
Vaibhav Vishal

Reputation: 7108

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

Upvotes: 1

Related Questions