How to read file with space separated values in pandas

PythonPandasCsv

Python Problem Overview


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

Python Solutions


Solution 1 - Python

add delim_whitespace=True argument, it's faster than regex.

Solution 2 - Python

you can use regex as the delimiter:

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

Solution 3 - Python

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionyemuView Question on Stackoverflow
Solution 1 - PythonHYRYView Answer on Stackoverflow
Solution 2 - Pythonuser2927197View Answer on Stackoverflow
Solution 3 - PythonPierzView Answer on Stackoverflow