python中的readlines函数_使用Python中的readlines()函数检查下一行的条件

I have input.txt file and output.txt file which are passed in argument in Python script. I am reading input file content using readline() function. Before I update to current line and write it to output file, I want to check some condition on upcoming lines as described below. Could you please provide me some guidance? Thank you.

I want to update current line with internal_account value (random number with 16 digits) from 11th location if line starts with 01065008 and following condition are met.

5th upcoming line starts with 06 and

line start with 06 has value as USD from line 6

input.txt

01065008200520P629658405456454

02BRYAN ANGUS 56425555643

0300000000000000000HUTS7858863

04PROSPECTUS ENCLOSYUSS574U623

05AS OF 05/13/20 45452366753

06Q47USDTFT 87845566765

input.txt file has pattern:

1st line will start with 010065008

2nd line will start with 02

...

6th line will start with 06

1st line will start with 010065008

...

我尝试了什么?

import random

import sys

infile=open(sys.argv[1], 'r')

lines=infile.readlines()

outfile=open(sys.argv[2], 'w')

internal_account = random.randint(1000000000000000,9999999999999999)

formattedStr = ''

for line in lines:

if line[0:8] == '01065008':

formattedStr='%s%s%s'%(line[0:10],internal_account,line[26:])

outfile.write(formattedStr)

else:

outfile.write(line)

outfile.close()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值