您好,请有人帮助我尝试根据第一个值“tocken”循环一个txt文件,它当前仅在我指定tocken时循环。例如{600001130260}文件中有多个tocken都在第一行[0]我希望它对每个tocken/行进行迭代并提取指定的信息
数据文件如下所示
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|2015/10/15
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
这是我的代码
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)
这是我当前的输出
('Total_due', '86,578.93\n')
('Total_due', '79,191.18\n')
我希望它能给出到期的总输出,而不需要我在[0]中指定tocken
是否要使用
Total Due
的所有值? 如果是这样,您可以简单地执行以下操作:结果将是:
第一个“令牌”似乎是唯一的标识符。 如果您想要csv导出和多列支持,您可以选择以下方式:
mycsv.csv
:相关问题 更多 >
编程相关推荐