Aishwarya nair
Aishwarya nair

Reputation: 1

Parse complex text files in python

I have a txt file in the below format

*15*EN*5562*CB*56198898~ZA*QS*70*EA~ZA*QA*3246*EA~ZA*QP*16020*EA~ZA*QT*12*EA
*16*EN*9966*CB*60526588~ZA*QS*148*EA~ZA*QA*4261*EA~ZA*QP*18000*EA~ZA*QT*6*EA
*17*EN*9973*CB*61540958~ZA*QS*133*EA~ZA*QA*3475*EA~ZA*QP*15600*EA~ZA*QT*12*EA
*18*EN*9980*CB*61540385~ZA*QS*24*EA~ZA*QA*2157*EA~ZA*QP*10062*EA

how do I convert this to csv file in the expected format?

line EN   CB            QS    QA    QP    QT 
15  5562  56198898~ZA   70   3246  16020   12
16  9966  60526588~ZA   148  4261  18000    6
17  9973  61540958~ZA   133  3475  15600   12
18  9980  61540385~ZA    24  2157  10062   NAN

Upvotes: 0

Views: 241

Answers (2)

GiovaniSalazar
GiovaniSalazar

Reputation: 2094

like this

import pandas as pd

df = pd.read_csv('myfile.csv',sep="*",engine='python')

print(df)

Upvotes: 1

Eric Wilson
Eric Wilson

Reputation: 59365

You have tagged this question with pandas but the csv module should handle this just fine.

Here is a slightly modified example from the documentation:

>>> import csv
>>> with open('eggs.csv', newline='') as csvfile:
...     spamreader = csv.reader(csvfile, delimiter='*')
...     for row in spamreader:
...         print(', '.join(row))
Spam, Spam, Spam, Spam, Spam, Baked Beans
Spam, Lovely Spam, Wonderful Spam

Upvotes: 0

Related Questions