Python Forum
CSV file escaping characters
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CSV file escaping characters
#1
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!
Reply
#2
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Split Characters As Lines in File quest_ 3 2,515 Dec-28-2020, 09:31 AM
Last Post: quest_
  get two characters, count and print from a .txt file Pleiades 9 3,376 Oct-05-2020, 09:22 AM
Last Post: perfringo
  Introduction to escaping characters in strings Geelong_crazy 1 2,854 Jul-18-2020, 06:58 PM
Last Post: DT2000
  Reading integers from a file; the problem may be the newline characters JRWoodwardMSW 2 1,972 Jul-14-2020, 02:27 AM
Last Post: bowlofred
  Remove escape characters / Unicode characters from string DreamingInsanity 5 13,726 May-15-2020, 01:37 PM
Last Post: snippsat
  Escaping '$' in pymongo raulp2301 0 1,640 Feb-14-2020, 03:48 PM
Last Post: raulp2301
  Replace characters from file in Python 2.7 melmouja 2 2,498 Feb-04-2019, 01:32 PM
Last Post: melmouja
  Problem with Polish characters in xlsx file Mikser 4 3,432 Nov-18-2018, 06:22 PM
Last Post: Gribouillis
  Escaping whitespace and parenthesis in filenames jehoshua 2 9,740 Mar-21-2018, 09:12 AM
Last Post: jehoshua
  need algorithm to strip non-ascii characters from LONG csv file hereathome 3 3,716 Jan-12-2018, 02:04 AM
Last Post: hereathome

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020