I try to read the file into pandas.
The file has values separated by space, but with different number of spaces
I tried:
pd.read_csv('file.csv', delimiter=' ')
but it doesn’t work
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
add delim_whitespace=True argument, it’s faster than regex.
Method 2
you can use regex as the delimiter:
pd.read_csv("whitespace.csv", header=None, delimiter=r"s+")
Method 3
If you can’t get text parsing to work using the accepted answer (e.g if your text file contains non uniform rows) then it’s worth trying with Python’s csv library – here’s an example using a user defined Dialect:
import csv
csv.register_dialect('skip_space', skipinitialspace=True)
with open(my_file, 'r') as f:
reader=csv.reader(f , delimiter=' ', dialect='skip_space')
for item in reader:
print(item)
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0