0

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
1
  • To get the output you want, you need to do some custom work. Typically one row in the csv is reserved for headers, in your csv its mixed in with the data so you need to parse that out :/ Commented Oct 22, 2019 at 17:48

2 Answers 2

1

like this

import pandas as pd

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

print(df)
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.