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()