I need to open a file.csv in Pandas. For that, I can use pd.read_csv('file.csv').
The problem is, the file is not properly formatted:
a b c
1 2 5
3 4 6
The first delimiter is 1 space and the second delimiter is 3 spaces.
I couldn't find a way on pandas documentation on how to do that.
I can pre process the file beforehand, transform it to a StringIO and open with pandas, but it seems hackish to me.
with open('file.csv', 'r') as f:
text = f.read()
text = text.replace(' ', ' ')
text = StringIO(text)
df = pd.read_csv(text)
How can I do that with pandas directly?
delim_whitespace=Trueinstead. More infosep='\s+'?sepinvokes the python parser which is slower. I'm not sure ifdelim_whitespacedoes the same but it is definitely more idiomatic.