2

I have got a text file containing reference, name, address, amount, dateTo, dateFrom and mandatory columns, in the following format:i would lke to pull the specific information then write the information to the corresponding columns

['HEADER', 'A000000209457', '20170706140003\n']
['10005369133', '000', '', '', '', '', 'MM', '', '', '', 
'[email protected]', '0836129535\n']

['10005369133', '150', '', '278.68', '2.05', '0.00', '1.93', '0.00', 
'282.66\n']
['TRAILER', 'A000000209457', '1', '282.66\n']

the output should look like this in a CSV FILE

Sub Total,VAT,Due Date,Previous Account Balance
280.73,0.00,2015/03/24,280.73

3 Answers 3

2

If you have the data in a specific format, like delimited by either a comma "," or a pipe "|" or any other character, you can read it directly in the pandas dataframe by specifying the delimiter and then extract the required data from it. This will give you the data in a tabular format.

import pandas as pd

df = pd.read_csv(r"path/to/file/filename.txt", sep='|')  
print(df)
Sign up to request clarification or add additional context in comments.

1 Comment

Hello can you please take another look updated initial
2

Have you considered using pandas? Here is a reference:

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_csv.html

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html

Comments

0

You could maybe use the split function

data = """HEADER|A000000209457|20170706140003
10005369133|000|||||MM||||[email protected]|0836129535
10005369133|005|||AC|2062|00||20170614164026
10005369133|010||E|552520062|6000782256|91115 5525200621|0146 552520062|2015/03/24|2015/03/24|282.66|14||R 0.00|213109|516008800111159 55252006201|||A000000209457
10005369133|020||SUMMERCON|PO Box 2595|LONEHILL|2062||"""
csv = ",".join(data.split("|"))
with open("data.csv", "w") as f:
    f.write(csv)

1 Comment

Hello i already do split the information i need to gather the data in the specific rows then write them to a csv using something like an if else

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.