1

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?

Gustavo Lopes
  • 3,794
  • 4
  • 17
  • 57

1 Answers1

2

Did you try pd.read_csv('file.csv', sep='\s+')?

Quang Hoang
  • 146,074
  • 10
  • 56
  • 74