Extract backslash and double quote data - Python CSV Reader

Below is my code to extract data from a csv file (I got the file from dumpped mysql).

data = csv.reader(f, delimiter=',', quotechar='"') 

After several tests, I found that my code above has one big problem. he cannot extract the data below:

 "25","Mike Ross","Tennok\"","NO" 

Any idea to fix this? TQ.

+4
source share
1 answer

The csv module expects the quotation mark to be doubled by default to indicate a literal " , so it will not correctly separate the fields ...

 data = csv.reader(f, delimiter=',', quotechar='"') # ['25', 'Mike Ross', 'Tennok\\",NO"'] 

Use escapechar to overcome this behavior:

 data = csv.reader(f, delimiter=',', quotechar='"', escapechar='\\') # ['25', 'Mike Ross', 'Tennok"', 'NO'] 
+4
source

Source: https://habr.com/ru/post/1500848/


All Articles