简体   繁体   English

在 Python 中使用 readlines() function 检查下一行的条件

[英]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.我有input.txt文件和output.txt文件,它们在 Python 脚本的参数中传递。 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.我正在使用readline() function 读取输入文件内容。在我更新到当前行并将其写入 output 文件之前,我想检查后续行的某些情况,如下所述。 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.如果行以01065008并满足以下条件,我想从第 11 个位置使用internal_account值(16 位随机数)更新当前行。

  1. 5th upcoming line starts with 06 and第 5 行从06开始,
  2. line start with 06 has value as USD from 6th character06开头的行从第 6 个字符开始的值为USD

input.txt输入.txt

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

input.txt file has pattern: input.txt文件有模式:

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.使用enumerate function 来跟踪线路索引。

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 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.您需要捕获IndexError以便在剩余行数不足时不会引发异常。

Other minor modifications I made in your code:我对您的代码所做的其他小修改:

  • Use with statement to handle file opening to prevent having to close file yourself.使用with语句来处理文件打开,以防止必须自己关闭文件。
  • Use startswith wherever you can to make the code clearer.尽可能使用startswith使代码更清晰。
  • 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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