Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 读取匹配字符串后的文件,直到文件结束_Python_Python 3.x - Fatal编程技术网

Python 读取匹配字符串后的文件,直到文件结束

Python 读取匹配字符串后的文件,直到文件结束,python,python-3.x,Python,Python 3.x,在匹配后,我正试图从文件中读取readline: with open(jij, "a") as jout: with open(jfile, "r") as jinp: for line in jinp: if line.strip().startswith("IQ"): # for _ in line: #for lines in jinp: for lines in range(2500): # li

在匹配后,我正试图从文件中读取
readline

with open(jij, "a") as jout:
  with open(jfile, "r") as jinp:
    for line in jinp:
      if line.strip().startswith("IQ"):
        # for _ in line:
        #for lines in jinp:
        for lines in range(2500):
          # lines = jinp.readline()
          rows = jinp.readline().split()
          print("{0:<3s}{1:<3s}{2:<3s}{3:<3s}{4:>3s}{5:>3s}{6:>3s}{7:>15s}{8:>7s}".
                format(rows[3], rows[2], rows[0], rows[1], rows[4], rows[5], rows[6], rows[11], rows[10]))
我试图打印几个元素作为列表后,它发现“智商”

我更喜欢的方法是通过
for uu-in-line
来完成,它只占用前100行<代码>用于jinp中的行跳过一行,然后读取下一行。只有当我把它放在射程内时,它才能按预期工作。但我不想固定电话号码

在线的
出了什么问题

是完整的文件

对于范围(2500)内的行,结果为

结果与
for_uuuuu一致

jinp中的行的结果为


预期结果来自范围(2500)
,但我不想硬编码行号。

您的问题是重复使用相同的fd:

  rows = jinp.readline().split()# This make the pointer point to next line
您所有的解决方案都有这一行+另一种方式的迭代:

# for _ in line: go over the chars in the line (100)
#for lines in jinp: go over the open file - > so you read twice per iteration
你可以用这个,更短,更易读

flag = False
with open(jij, "a") as jout:
    with open(jfile, "r") as jinp:
        for line in jinp:
            if flag:
                rows = line.split()
                jout.write("{0:<3s}{1:<3s}{2:<3s}{3:<3s}{4:>3s}{5:>3s}{6:>3s}{7:>15s}{8:>7s}\n".
                           format(rows[3], rows[2], rows[0], rows[1], rows[4], rows[5], rows[6], rows[11],
                                  rows[10]))
            else:
                flag = line.strip().startswith("IQ") 
flag=False
以open(jij,“a”)作为jout:
以open(jfile,“r”)作为jinp:
对于jinp中的行:
如果标志:
行=行。拆分()
jout.write(“{0:15s}{8:>7s}\n”。
格式(行[3]、行[2]、行[0]、行[1]、行[4]、行[5]、行[6]、行[11],
第[10]行)
其他:
flag=line.strip().startswith(“IQ”)

您的问题是重复使用相同的fd:

  rows = jinp.readline().split()# This make the pointer point to next line
您所有的解决方案都有这一行+另一种方式的迭代:

# for _ in line: go over the chars in the line (100)
#for lines in jinp: go over the open file - > so you read twice per iteration
你可以用这个,更短,更易读

flag = False
with open(jij, "a") as jout:
    with open(jfile, "r") as jinp:
        for line in jinp:
            if flag:
                rows = line.split()
                jout.write("{0:<3s}{1:<3s}{2:<3s}{3:<3s}{4:>3s}{5:>3s}{6:>3s}{7:>15s}{8:>7s}\n".
                           format(rows[3], rows[2], rows[0], rows[1], rows[4], rows[5], rows[6], rows[11],
                                  rows[10]))
            else:
                flag = line.strip().startswith("IQ") 
flag=False
以open(jij,“a”)作为jout:
以open(jfile,“r”)作为jinp:
对于jinp中的行:
如果标志:
行=行。拆分()
jout.write(“{0:15s}{8:>7s}\n”。
格式(行[3]、行[2]、行[0]、行[1]、行[4]、行[5]、行[6]、行[11],
第[10]行)
其他:
flag=line.strip().startswith(“IQ”)

您能发布您的输出和所需的输出吗?@t.m.adam:added由于
len(line)
为100,因此为uuuin line打印100行。如果你想打印“IQ”之后的行,你可以把它们放在一个列表中,然后反复迭代。你能发布你的输出和所需的输出吗?@t.m.adam:addedThe
for uuuuu-in-line
打印100行,因为
len(line)
是100。如果你想在“IQ”之后打印这些行,你可以把它们放在一个列表中,然后在上面迭代