python处理txt文件

时间:2022-12-20 13:14:07

需求:

给一个txt文件,经过程序处理后,生成目标txt文件

例如:

原文件内容如下:

商户号|终端号|交易类型|交易子类型|总笔数|总金额|总手续费|清算时间
1201603|38554|04317|00|13|0.31|2.40|2018-01-15
商户号|终端号|交易类型|交易子类型|宝付订单号|商户订单号|清算日期|订单状态|交易金额|手续费|宝付交易号|支付订单创建时间|商户退款订单号|退款订单创建时间
1201603|38554|04317|00|1574960674|S180115121807148001|2018-01-15|1|0.02|0.80|201801150110001574960674|2018-01-15 12:18:07||
1201603|38554|04317|00|1574960674|S180115121807148001|2018-01-15|1|0.01|0.00|201801150110001574960674|2018-01-15 12:18:07||
1201603|38554|04317|00|1574988830|S180115122645148008|2018-01-15|1|0.01|0.00|201801150110001574988830|2018-01-15 12:26:45||
1201603|38554|04317|00|1574988830|S180115122645148008|2018-01-15|1|0.03|0.80|201801150110001574988830|2018-01-15 12:26:45||
1201603|38554|04317|00|1574992052|S180115122753148009|2018-01-15|1|0.03|0.80|201801150110001574992052|2018-01-15 12:27:54||
1201603|38554|04317|00|1574992052|S180115122753148009|2018-01-15|1|0.02|0.00|201801150110001574992052|2018-01-15 12:27:54||
1201603|38554|04317|00|1574992052|S180115122753148010|2018-01-15|1|10.00|1.00|201801150110001574992053|2018-01-15 12:27:54||
1201603|38554|04317|00|1574992052|S180115122753148011|2018-01-15|1|20.00|0.00|201801150110001574992054|2018-01-15 12:27:54||

处理后的新文件:

商户号|终端号|交易类型|交易子类型|总笔数|总金额|总手续费|清算时间
1201603|38554|04317|00|13|0.31|2.40|2018-01-15
商户号|终端号|交易类型|交易子类型|宝付订单号|商户订单号|清算日期|订单状态|交易金额|手续费|宝付交易号|支付订单创建时间|商户退款订单号|退款订单创建时间|分账金额
1201603|38554|04317|00|1574960674|S180115121807148001|2018-01-15|1|0.02|0.80|201801150110001574960674|2018-01-15 12:18:07|||0.01
1201603|38554|04317|00|1574988830|S180115122645148008|2018-01-15|1|0.03|0.80|201801150110001574988830|2018-01-15 12:26:45|||0.01
1201603|38554|04317|00|1574992052|S180115122753148009|2018-01-15|1|0.03|0.80|201801150110001574992052|2018-01-15 12:27:54|||0.02
1201603|38554|04317|00|1574992052|S180115122753148010|2018-01-15|1|10.00|1.00|201801150110001574992053|2018-01-15 12:27:54|||0.00
1201603|38554|04317|00|1574992052|S180115122753148011|2018-01-15|1|20.00|0.00|201801150110001574992054|2018-01-15 12:27:54|||0.00

程序如下:

#-*— coding:utf-8 -*-
#处理txt文件
__author__ = 'zengqiang.wang'

#打开文件
file=open('fi_dz_1201603_2018-01-15_new.txt')

#存放文件前二行
dest1=[]
#存放文件其余的行
dest2=[]

#将文件内容存放到相应的变量中
for f in file:
f1 = f.split('|')#以|进行字符串的分割
if len(f1) == 8:
dest1.append(f1)
else:
dest2.append(f1)
dest=[]#存放处理后的文件内容的变量
#处理宝付订单号成对出现的。
for i in range(len(dest2)):
if dest2[i][0] == '商户号':
dest2[0].append('分账金额')#对第三行的处理
else:#对剩下几行的处理,合并某两行上午订单号相同的,然后将手续费为0的追加加到不为0的后面
dest2[i][13] = ''#处理换行
for d1 in dest2:
if d1[5] == dest2[i][5] and d1[9] == '0.00' and dest2[i][9] != '0.00':
dest2[i].append(d1[8])
dest.append(dest2[i])




#目标生成文件,里面包含处理后的内容
file=open('dest.txt','w')
print dest1
print dest2[0]
print dest

#处理宝付订单号不成对的。
orderIds1=[]
#把成对的宝付订单号存放在orderIds1变量中
for j in range(len(dest)):
orderIds1.append(dest[j][5])
#print orderIds1

dest3=[]#把不成对的行存放在dest3里面。
for k in range(len(dest2)):
if dest2[k] == dest2[0]:
continue
if dest2[k][5] not in orderIds1:
dest3.append(dest2[k])

#处理list中的字符串中包含的换行符\n
for j in range(len(dest3)):
for i in range(len(dest3[j])):
dest3[j][i] = dest3[j][i].strip('\n')
for m in range(len(dest3)):
dest3[m].append('0.00')
dest.append(dest3[m])


#遍历将变量存入文件
#将前两行写入文件
for d in dest1:
for i in range(len(d)):
d[i] = d[i].strip('\n')
if i < len(d)-1:
d[i] += '|'
file.write(d[i])
file.write('\n')
#将第三行写入文件
for i in range(len(dest2[0])):
dest2[0][i] = dest2[0][i].strip('\n')
if i < len(dest2[0])-1:
dest2[0][i] += '|'
file.write(dest2[0][i])
file.write('\n')

#将其余几行写入文件
for d in dest:
for i in range(len(d)):
if i < len(d)-1:
d[i] += '|'
file.write(d[i])
file.write('\n')