yemu
yemu

Reputation: 28259

How to read file with space separated values in pandas

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

Upvotes: 156

Views: 232978

Answers (5)

HYRY
HYRY

Reputation: 97291

Add delim_whitespace=True argument, it's faster than regex.
Note however that after pandas 2.2.0 this is deprecated and it's better to use sep="\s+"

Upvotes: 248

Don Feto
Don Feto

Reputation: 1484

You can pass a regular expression as a delimiter for read_table also, and it is fast :).

result = pd.read_table('file', sep='\s+')

Upvotes: 3

erickfis
erickfis

Reputation: 1204

Pandas read_fwf for the win:

import pandas as pd

df = pd.read_fwf(file_path)

Upvotes: 7

Pierz
Pierz

Reputation: 8118

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)

Upvotes: 0

user2927197
user2927197

Reputation:

you can use regex as the delimiter:

pd.read_csv("whitespace.csv", header=None, delimiter=r"\s+")

Upvotes: 56

Related Questions