简体   繁体   中英

How to extract specific data from a text file and write into CSV using python

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', '', '', '', 
'nanm@summercon.co.za', '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

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)

You could maybe use the split function

data = """HEADER|A000000209457|20170706140003
10005369133|000|||||MM||||nanm@summercon.co.za|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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM