在python中匹配后跳过2行

时间:2014-10-09 11:12:15

标签: python

我想在比赛结束后跳过2行,如图所示。 我试过skipping a line while reading a file with a for loop 这给了我错误:

 fin.next()
AttributeError: '_io.TextIOWrapper' object has no attribute 'next'

粘贴我的完整代码,输入,输出和预期输出。我是新手。请帮助。

#!/usr/bin/python3     fin.next()
AttributeError: '_io.TextIOWrapper' object has no attribute 'next'
import sys
inp = str(sys.argv[1])
print(inp)

mesh = []
with open(inp, 'r') as fin:
    for line in fin:
        if line.startswith("NQ"):
            nq = int(line[6:].strip())
        if line.startswith("NT"):     # Number of elements
            nt = int(line[6:].strip())
        if line.startswith("NM"):     # Number of type
            nm = int(line[6:].strip())
        if line.startswith("IREL"):
            irel = line[6:].strip()
        if line.startswith("NSPIN"):
            nspin = line[6:].strip()


# Getting the Mesh
        if line.startswith("MESH-TYPE EXPONENTIAL"):
            fin.readline(1)
            for i in range(nm):
                mesh = line.split()
                print(mesh)

输入:

*******************************
GLOBAL SYSTEM PARAMETER
NQ                 2
NT                 2
NM                 2
IREL               3
NSPIN              1
*******************************************************************************
MESH INFORMATION
MESH-TYPE EXPONENTIAL 
   IM       R(1)                  DX              JRMT       RMT             JRWS       RWS
    1  1.00000000000000E-06  2.02543202000000E-02  716  1.94855715851499E+00  721  2.15458905088026E+00
    2  1.00000000000000E-06  2.03289742000000E-02  713  1.94855715851499E+00  721  2.27356898717917E+00
*******************************************************************************

输出:

['MESH-TYPE', 'EXPONENTIAL']
['MESH-TYPE', 'EXPONENTIAL']

预期产出:

1  1.00000000000000E-06  2.02543202000000E-02  716  1.94855715851499E+00  721  2.15458905088026E+00
2  1.00000000000000E-06  2.03289742000000E-02  713  1.94855715851499E+00  721  2.27356898717917E+00

0 个答案:

没有答案