Space delimiter in CSV import to Python

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Space delimiter in CSV import to Python



I know there are more than a few questions regarding space delimiters in CSV files.



I have a CSV file that appears to be separated by a space. When importing to Python, I have tried every code out there to identify space as a delimiter. However, I keep getting error messages. For example:


test_filepath = 'test_data.csv'

with codecs.open(test_filepath, "r", "Shift-JIS", "ignore") as file: # import UTF8 based csv file
test_df = pd.read_table( file, delim_whitespace=True )



this yields the following error:


EmptyDataError: No columns to parse from file



when I try this:


test_filepath = 'test_data.csv'

with codecs.open(test_filepath, "r", "Shift-JIS", "ignore") as file: # import UTF8 based csv file
test_df = pd.read_table( file, delimiter=" " )



it gives the same error.



when i try this:


test_filepath = 'test_data.csv'

with codecs.open(test_filepath, "r", "Shift-JIS", "ignore") as file: # import UTF8 based csv file
test_df = pd.read_table( file, sep = "/s+" )



I get the same error.



When I try this:


test_filepath = 'test_data.csv'

with codecs.open(test_filepath, "r", "Shift-JIS", "ignore") as file: # import UTF8 based csv file
test_df = pd.read_table( file, delimiter='t')



I get the same error.



the ONLY WAY I dont get an error is if I do this:


test_filepath = 'test_data.csv'

with codecs.open(test_filepath, "r", "Shift-JIS", "ignore") as file: # import UTF8 based csv file
test_df = pd.read_table( file, delimiter=',')



but the results look completely off, and test_df.info() shows that only one column is created (there should be like 100 columns).





does it have headers?
– ubuntu_noob
Jul 27 at 1:14





How do you know your data is space-delimited? Have you opened your data and looked at? Your traceback seems to think you don't have any data.
– Joel
Jul 27 at 1:14





open it in excel and make it space delimited
– ubuntu_noob
Jul 27 at 1:17





Instead of wildly guessing at what kind of whitespace you have, why not actually read one of the rows (just row = file.readline()) and then print repr(row) and actually see what you have?
– abarnert
Jul 27 at 1:23


row = file.readline()


print repr(row)





Also, you need to read the docs more carefully. /s+ doesn't mean anything—or, rather, it means you want to separate columns with a literal slash and then one or more letter ss, which isn't very useful.
– abarnert
Jul 27 at 1:25


/s+


s




1 Answer
1



I think pandas might do the trick, one of these should work.


import pandas as pd

df = pd.read_csv('file.csv', delim_whitespace=True)
df = pd.read_csv('file.csv', delimiter=' ')






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

C# - How to create a semi transparent or blurred backcolor on windows form

Swipe gestures in WKWebView

How to populate data on nav-tab in partial View in MVC?