我正在尝试在python中匹配后转到下一行。我有一个文件:
ratio of lattice parameters b/a c/a
1.000000000000 1.000000000000
lattice parameters a b c [a.u.]
9.448629942895 9.448629942895 9.448629942895
Run Code Online (Sandbox Code Playgroud)
因此,当我"lattice parameters"
在行的开头时,我将阅读9.448629942895 9.448629942895 9.448629942895
我正在做的下一行(例如 ):
#!/usr/bin/python3
import sys
inpf = str(sys.argv[1])
print (inpf)
with open(inpf, "r") as ifile:
for line in ifile:
if line.startswith("lattice parameters"):
print(line.strip())
next(ifile)
return next(ifile)
Run Code Online (Sandbox Code Playgroud)
就像这里接受的答案一样。但是,就我而言,它给出了错误:
python xband.py AlSi2S2.sys
File "xband.py", line 11
return next(ifile)
SyntaxError: 'return' outside function
Run Code Online (Sandbox Code Playgroud)
我在这里做错了什么?
注意:我想去阅读下一行。我对使用的“返回”没有特别的兴趣Python 3.4.2
尝试:
with open(inpf, "r") as ifile:
for line in ifile:
if line.startswith("lattice parameters"):
print(next(ifile, '').strip())
Run Code Online (Sandbox Code Playgroud)
next(iterator [,default])
通过调用迭代器的next()方法从迭代器中获取下一个项目。如果给定默认值,则在迭代器耗尽时返回它,否则引发StopIteration。
归档时间: |
|
查看次数: |
11391 次 |
最近记录: |