Marc Schwambach
Marc Schwambach

Reputation: 438

Reading unusual .txt text format in pandas

My question is really straight forward however I am not sure if pandas or any other library has a way of dealing with such issue. The problem is that I have a .txt file with format as bellow (including the text "This is the example..."). So, as I want to read multiple files with that format and keep appending then to one dataframe. Hence, I was wondering if there is a simple way to read it despite the uneven format without separation by commas or decimal separator.

Hope that I managed to be succinct and precise. I would really appreciate your help on this one! Suggestions of what to look up for are also welcome.

I utilized notepad C++ to open the file. I also uploaded the file here in case someone wants to test it.

enter image description here

Upvotes: 0

Views: 650

Answers (1)

gosuto
gosuto

Reputation: 5741

You can treat it as a .csv file but using tabs (\t) as a delimiter opposed to commas.

pd.read_csv('txt_example.txt', sep='\t', skiprows=2) returns a beautiful dataframe for me!

Edit: as suggested below the header argument creates an even better output; we can make sure the second row is not seen as a data row:

df = pd.read_csv('txt_example.txt', sep='\t', skiprows=2, header=[0,1])
df.head()
Time    RtAxsXT1    RtAxsYT1    RtAxsZT1    RtPosXT1    RtPosYT1    RtPosZT1    YawErrT1    TIAmbT1 CtT1N01 ... WkDfVrT3N17D7   WkDfVrT3N18D7   WkDfVrT3N19D7   WkDfVrT3N20D7   W3VAmbX W3VAmbY W3VAmbZ W3VDisX W3VDisY W3VDisZ
(s) (-) (-) (-) (m) (m) (m) (deg)   (percent)   (-) ... (m/s)   (m/s)   (m/s)   (m/s)   (m/s)   (m/s)   (m/s)   (m/s)   (m/s)   (m/s)
0   0.0 0.9962  0.0 -0.08716    995.0   1000.0  90.0    0.3525  12.28   0.0 ... 0.0 0.0 0.0 0.0 5.902   -0.09767    -0.009375   5.684   -0.09767    0.00971
1   2.0 0.9962  0.0 -0.08716    995.0   1000.0  90.0    1.4390  12.17   0.0 ... 0.0 0.0 0.0 0.0 5.872   -0.16040    0.283000    5.653   -0.16210    0.30040
2   4.0 0.9962  0.0 -0.08716    995.0   1000.0  90.0    0.9589  12.00   0.0 ... 0.0 0.0 0.0 0.0 6.031   -0.11650    0.593000    5.812   -0.11820    0.61040
3   6.0 0.9962  0.0 -0.08716    995.0   1000.0  90.0    2.0930  11.96   0.0 ... 0.0 0.0 0.0 0.0 6.245   0.24430 0.733700    6.025   0.24260 0.75120
4   8.0 0.9962  0.0 -0.08716    995.0   1000.0  90.0    3.1810  12.12   0.0 ... 0.0 0.0 0.0 0.0 6.483   0.79060 0.681000    6.261   0.78890 0.69870
5 rows × 1069 columns

Upvotes: 4

Related Questions