Darth BEHFANS
Darth BEHFANS

Reputation: 419

Pandas read part of a delimited txt file

So I have a txt file from the earthquake experiment machine output:

23      (-9.144,  2.7432,  0) [HybridDamper/Floor 1 Disp m,--,--] #ffffff
24      (-4.572,  0,       0) #ffffff
# End Node section
===
# Begin Member section
gl gr #eeeeee
23 D1 yellow

So there is a # End Node section, ===, #Begin Member secion that separates two parts of the output. I want to create two dataframes, one for the above part, and the other one for the below part.

Without manually edit the output file from machine (which is not easy when the structure is big), is it possible to let pandas recognize the part separator (e.g., these lines start from #, =) from the .txt file?

Thank you guys! Best Shawn

Upvotes: 2

Views: 3510

Answers (1)

piRSquared
piRSquared

Reputation: 294218

from io import StringIO
import pandas as pd

with open('mytext.txt') as f:
    txt = f.read()

marker = '# End Node section\n===\n# Begin Member section'
txt1, txt2 = txt.split(marker)

df1 = pd.read_csv(StringIO(txt1), header=None, sep='|')
df2 = pd.read_csv(StringIO(txt2), header=None, sep='\s+', engine='python')

Upvotes: 3

Related Questions