首页 > 解决方案 > 如何访问字节数组文件的某些字节?

问题描述

我有一个文本文件,其中每一行都包含 bytearray。我想打印第二个和第三个数字等于 34 的行并计算它们以显示此文件中存在多少。我的 file.txt 的内容是:

12345678
12678364
90783457
95349387

python 脚本应该打印该文件的第一行和最后一行。为此,我写道:

count =0
with open ('file.txt', 'rt') af in_file:
    for line in in_file:
        if line[2:4]=='34':  #problem is in this line. this line prints line numbers not value of each line.
            print("line: ", line)
            count +=1
        print("count: ", count)

我不知道如何编写该if语句才能正常工作。

标签: pythonfile

解决方案


推荐阅读