Python Forum
CSV file escaping characters - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: CSV file escaping characters (/thread-23065.html)



CSV file escaping characters - lokhtar - Dec-09-2019

Hello,

I am trying to do something relatively simple - load a csv file and create a list (or a dataframe). The separator for my file is "|", but the data in my csv file can have all the other characters - ",':, space, etc. Need to ignore them all - but the code that I have messes up when looking through that file. Any help would be appreciated!

#load dataframe from csv
import csv
import pandas as pd

df1 = pd.read_csv('reports/reports_test.csv', delimiter='|')
print(df1)

I am still not able to upload files (do not have five posts yet), so I am posting the sample CSV:

Quote:1|2|3|4
"A zasdff|B sdafasfd,as dfsadf""|C & D are here|D
More data|12, 124, 12|weird delimiters:'no way|stuff
12|><(yes|111|stuff and other stuff


What I need to have is a table/list that look like this:

[Image: final-output.png]

Basically, it should ignore all characters other than "|" when creating each of the fields (theoretically the CSV field can have tabs, newlines, etc as well).

I would appreciate any help!


RE: CSV file escaping characters - buran - Dec-09-2019

(Dec-09-2019, 06:06 PM)lokhtar Wrote: theoretically the CSV field can have tabs, newlines, etc as well
are you sure about the newlines? If you have newline inside a field you will need to write own parser it still could be tricky


RE: CSV file escaping characters - lokhtar - Dec-09-2019

Hmm, well there actually might be. But I might be able to manually remove that if necessary prior to reading the file.