简体   繁体   中英

Check condition on upcoming line using readlines() function in Python

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.

  1. 5th upcoming line starts with 06 and
  2. line start with 06 has value as USD from 6th character

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
...

What I have tried?

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

To check forward in the text file, read all the lines into a list then use the line index to check forward lines. Use the enumerate function to the track the line index.

ss = '''
01065008200520P629658405456454
02BRYAN ANGUS      56425555643
0300000000000000000HUTS7858863
04PROSPECTUS ENCLOSYUSS574U623
05AS OF 05/13/20   45452366753
06Q47USDTFT        87845566765
'''.strip()
with open ('input.txt','w') as f: f.write(ss)  # write data file

###############################3

import random
import sys

infile=open('input.txt')   #open(sys.argv[1], 'r')
lines=infile.readlines()

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

internal_account = random.randint(1000000000000000,9999999999999999)
print('internal_account', internal_account, end='\n\n')

formattedStr = ''

for i,line in enumerate(lines):
    line
    if line[0:8] == '01065008' and i < len(lines)-5 and lines[i+5].startswith('06') and lines[i+5][5:8] == 'USD':
        formattedStr='%s%s%s'%(line[0:10],internal_account,line[26:])
        outfile.write(formattedStr)
        print(formattedStr.strip())
    else:
         outfile.write(line)
         print(line.strip())
outfile.close()

Output

internal_account 2371299802657810

010650082023712998026578106454
02BRYAN ANGUS      56425555643
0300000000000000000HUTS7858863
04PROSPECTUS ENCLOSYUSS574U623
05AS OF 05/13/20   45452366753
06Q47USDTFT        87845566765

You were not far from finding a good solution. Using enumerate on input lines let use use the index to check future lines so you can verify if all your conditions are fulfilled. You need to catch IndexError so that no exception is raised when there are not enough lines left.

Other minor modifications I made in your code:

  • Use with statement to handle file opening to prevent having to close file yourself.
  • Use startswith wherever you can to make the code clearer.
  • Use scientific notation when you can to make code clearer.
import random
import sys

input_file, output_file = sys.argv[0:2]
internal_account = random.randint(1e15, 9999999999999999)

with open(input_file, "r") as stream:
    input_lines = stream.readlines()

with open(output_file, "w") as stream:
    for index, line in enumerate(input_lines):
        try:
            update_account = (
                line.startswith("01065008")
                and input_lines[index + 5].startswith("06")
                and input_lines[index + 5][5:8] == "USD"
            )
        except IndexError:
            update_account = False

        if update_account:
            line = line[0:10] + str(internal_account) + line[26:]

        stream.write(line)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM