Good day may someone please help im trying to loop through a txt file based on the first value"tocken",it currently loops only if i specify the tockens e.g {600001130260} there are multiply tockens in the file all in the first row[0] id like it to iterate for each tocken/row and extract the specified info.
The data file looks like this
600001130260|005|||IN|2197|01||20160905210028
600001130260|100|005|00|VAT|VAT|VAT @ 14%|2,150.14
600001130260|100|013|00|TOT|CTOT|Total Due|86,578.93
600001130260|100|014|00|DD|DD|Due Date|2015/09/22
600001130260|200|019|01||YDACON|Daily average consumption 79.325 kWh||
28002385859|000|||||LT|||T0IQ04960000000016400000000000000||
28002385859|100|005|00|CUR|CUR|Current Charges (Excl. VAT)|304.48
28002385859|100|006|00|VAT|VAT|VAT @ 14%|10.62
28002385859|100|013|00|TOT|CTOT|Total Due|26,451.75
28002385859|100|014|00|DD|DD|Due Date|2015/09/23
28002385859|150||23,149.02|1,686.37|1,233.57|382.79|0.00|26,451.75
112002209769|100|004|00|CUR|CUR|Current Charges (Excl. VAT)|2,790.25
112002209769|100|005|00|VAT|VAT|VAT @ 14%|353.30
112002209769|100|013|00|TOT|CTOT|Total Due|3,483.28
112002209769|100|014|00|DD|DD|Due Date|2015/09/23
100002232416|100|013|00|TOT|CTOT|Total Due|983.04
100002232416|100|014|00|DD|DD|Due Date|2015/09/23
142002121343|100|005|00|VAT|VAT|VAT @ 14%|239.80
142002121343|100|013|00|TOT|CTOT|Total Due|- 197,358.33
142002121343|300|011|00||VAT|VAT: 14.00%|4.70|38.29
This is my code
file1 = open(r"C:\Users\isaac.gumbi\Documents\jhb\Full test file.txt", 'r')
file2 = ""
with file1 as f:
for line in f:
tokens = line.split("|")
keys = {'600001130260','118002191517','CTOT', 'CUR', 'Due Date',
'VAT', '020', '030', '010', '040', 'STOT', '000', '005',
'050', '0100', 'BBF', 'INT','CIN', 'CTOT', 'DD', 'YVLEVY',
'YRREM'}
if len(tokens) and tokens[0] in keys and tokens[5] == 'CTOT':
Total_due = ' '.join(tokens[7:8])
if Total_due == '' : Total_Due = "null"
print ("Total_due", Total_due)
This is my current output
('Total_due', '86,578.93\n')
('Total_due', '79,191.18\n')
i would like it to give me the output total_due without me specifying the "tocken[0] in keys".becuase the file will contain 1000's of records and they will each be using a uninque tocken[0]
Aucun commentaire:
Enregistrer un commentaire