Python,捕获文本文件中的值
我有一个文件(fluiddynamic代码的输出),其中我希望在以Python,捕获文本文件中的值,python,get,Python,Get,我有一个文件(fluiddynamic代码的输出),其中我希望在以m longest开头的行后面写入值 以下是相关线路的报告: dilute dimensional Zimm longest relaxation time, dil_chtime= 3.29486769328041 到目前为止,我只写了这句话: 但我不知道为什么它没有捕捉到值3.2948676932 zimm = 0. with open('memo.dat','r') as f: for li
m longest开头的行后面写入值
以下是相关线路的报告:
dilute dimensional Zimm longest relaxation time, dil_chtime=
3.29486769328041
到目前为止,我只写了这句话:
但我不知道为什么它没有捕捉到值3.2948676932
zimm = 0.
with open('memo.dat','r') as f:
for line in f.readlines() :
if(line.startswith(' dilute dimensional Zimm longest')):
print (line)
zimm = f.readline() # I suppose that this read the next line
else:
pass
此帧的输出为:
最长弛豫时间,dil_chtime=
如何获取值?memo.dat:
dilute dimensional Zimm longest relaxation time, dil_chtime=
3.29486769328041
Python 2.x:
zimm = 0.
nextLine = False # a boolean flag to get the next line
with open('memo.dat', 'r') as f:
content = f.readlines()
# you may also want to remove empty lines
content = [l.strip() for l in content if l.strip()]
for line in content:
try:
if (line.startswith('dilute dimensional Zimm longest')):
nextLine = not nextLine
elif nextLine:
print(line)
nextLine = not nextLine
except StopIteration:
pass
3.29486769328041
输出:
zimm = 0.
nextLine = False # a boolean flag to get the next line
with open('memo.dat', 'r') as f:
content = f.readlines()
# you may also want to remove empty lines
content = [l.strip() for l in content if l.strip()]
for line in content:
try:
if (line.startswith('dilute dimensional Zimm longest')):
nextLine = not nextLine
elif nextLine:
print(line)
nextLine = not nextLine
except StopIteration:
pass
3.29486769328041
Python 3.x:
zimm = 0.
nextLine = False # a boolean flag to get the next line
with open('memo.dat', 'r') as f:
content = f.readlines()
# you may also want to remove empty lines
content = [l.strip() for l in content if l.strip()]
for line in content:
try:
if (line.startswith('dilute dimensional Zimm longest')):
nextLine = not nextLine
elif nextLine:
print(line)
nextLine = not nextLine
except StopIteration:
pass
3.29486769328041
使用next()
:
memo.dat:
dilute dimensional Zimm longest relaxation time, dil_chtime=
3.29486769328041
Python 2.x:
zimm = 0.
nextLine = False # a boolean flag to get the next line
with open('memo.dat', 'r') as f:
content = f.readlines()
# you may also want to remove empty lines
content = [l.strip() for l in content if l.strip()]
for line in content:
try:
if (line.startswith('dilute dimensional Zimm longest')):
nextLine = not nextLine
elif nextLine:
print(line)
nextLine = not nextLine
except StopIteration:
pass
3.29486769328041
输出:
zimm = 0.
nextLine = False # a boolean flag to get the next line
with open('memo.dat', 'r') as f:
content = f.readlines()
# you may also want to remove empty lines
content = [l.strip() for l in content if l.strip()]
for line in content:
try:
if (line.startswith('dilute dimensional Zimm longest')):
nextLine = not nextLine
elif nextLine:
print(line)
nextLine = not nextLine
except StopIteration:
pass
3.29486769328041
Python 3.x:
zimm = 0.
nextLine = False # a boolean flag to get the next line
with open('memo.dat', 'r') as f:
content = f.readlines()
# you may also want to remove empty lines
content = [l.strip() for l in content if l.strip()]
for line in content:
try:
if (line.startswith('dilute dimensional Zimm longest')):
nextLine = not nextLine
elif nextLine:
print(line)
nextLine = not nextLine
except StopIteration:
pass
3.29486769328041
使用next()
:
您可以使用next(f)
获取下一行
Ex:
with open('memo.dat') as f:
for line in f: #Iterate Each Line
if line.strip().startswith('dilute dimensional Zimm longest'): #Check Condition
print(next(f)) #Get Value
break
您可以使用next(f)
获取下一行
Ex:
with open('memo.dat') as f:
for line in f: #Iterate Each Line
if line.strip().startswith('dilute dimensional Zimm longest'): #Check Condition
print(next(f)) #Get Value
break
您应该改为执行zimm=f.next()
。@RickM。首先,这是行不通的,因为文件已经被f.readlines()
完全读取了,其次,你真的应该做next(f)
,因为它与python 3兼容。@Aran Fey True,谢谢!您应该改为执行zimm=f.next()
。@RickM。首先,这是行不通的,因为文件已经被f.readlines()
完全读取了,其次,你真的应该做next(f)
,因为它与python 3兼容。@Aran Fey True,谢谢!您好,您不应该在循环的每次迭代之前放置nextLine=False
?因为这样它也可以用于多行。@Vaibhavgusain是的,但是在else
块中。非常感谢。您好,您不应该在循环的每次迭代之前放置nextLine=False
?因为这样它也可以用于多行。@Vaibhavgusain是的,但是在else
块中。非常感谢。