0

For example I have a file like below:

ex.txt

1 2

3 4 5

2

I want to read this file using pandas' read_table()

import pandas as pd
df = pd.read_table("ex.txt", sep = " ", header = None)

However, this code display tokenizing error. I wish to read all data, so I don't hope to use bad_line_error = False.

The number of my real files are 24 and each file's size is over 1GB. So I cannot modify all files manually.

I look forward to the results below:

1 2 Nan

3 4 5

2 NaN NaN

lksj
  • 29
  • 6
  • https://stackoverflow.com/questions/55129640/read-csv-into-a-dataframe-with-varying-row-lengths-using-pandas/55129746#55129746 will probably solve your problem. Just split on a space after you read in the lines entirely. You'll need to typecast the columns afterwords though. – ALollz Apr 29 '21 at 14:42
  • `df = pd.read_fwf("ex.txt", sep = " ", header = None)`. – Ynjxsjmh Apr 29 '21 at 14:48

0 Answers0