逐行打印第 n 行
Print nth line after line
我正在尝试打印文件中特定行之后的第 n 行:
import glob
import numpy as np
import matplotlib.pyplot as plt
import math
velerror = []
divergence = []
mesh = []
timestep = []
totaltime = []
for files in glob.glob('*.out'):
f = open(files, 'r')
for line in f:
if "Maximum Velocity Error:" in line:
velerror.append(float(line[32:40]))
if "Grid Dimensions, Mesh" in line:
mesh.append(int(line[27]))
if "Time Step " in line:
timestep.append(int(line[17:27].strip()))
if "Total time:" in line:
totaltime.append(float(line[47:57].strip()))
#Print nth line after line
totaltime = totaltime[0::9]
print(velerror)
print(mesh)
print(timestep)
print(totaltime)
是否有像 next() 这样的函数可以转到下第 n 行?
使用readlines()
i = 0;
lines = f.readlines()
while 1:
line = lines[i]
if not line:
break
if "Maximum Velocity Error:" in line:
velerror.append(float(line[32:40]))
if "Grid Dimensions, Mesh" in line:
mesh.append(int(line[27]))
if "Time Step " in line:
timestep.append(int(line[17:27].strip()))
if "Total time:" in line:
totaltime.append(float(line[47:57].strip()))
i = i + 8
i++
(请参阅没有 readlines
、live demo 的版本的编辑)
print the nth line after a specific line in a file
如果文件适合内存使用 readlines
和切片:
def print_after(source, specific_line_number, skip_lines):
for line in source[specific_line_number::skip_lines]:
print(line)
lines = [f'line {i:2}' for i in range(30)] # readlines here
print_after(lines, 7, 2)
在您的代码中:
import glob
import numpy as np
import matplotlib.pyplot as plt
import math
velerror = []
divergence = []
mesh = []
timestep = []
totaltime = []
for files in glob.glob('*.out'):
f = open(files, 'r')
for line in f:
if "Maximum Velocity Error:" in line:
velerror.append(float(line[32:40]))
if "Grid Dimensions, Mesh" in line:
mesh.append(int(line[27]))
if "Time Step " in line:
timestep.append(int(line[17:27].strip()))
if "Total time:" in line:
totaltime.append(float(line[47:57].strip()))
for line in islice
break # Stop consuming lines (?)
totaltime = totaltime[0::9]
print(velerror)
print(mesh)
print(timestep)
print(totaltime)
编辑:按照 rassahah 的建议使用 islice 可以帮助您节省一些内存:
from itertools import islice
import glob
import numpy as np
import matplotlib.pyplot as plt
import math
velerror = []
divergence = []
mesh = []
timestep = []
totaltime = []
for files in glob.glob('*.out'):
f = open(files, 'r')
for line in f:
if "Maximum Velocity Error:" in line:
velerror.append(float(line[32:40]))
if "Grid Dimensions, Mesh" in line:
mesh.append(int(line[27]))
if "Time Step " in line:
timestep.append(int(line[17:27].strip()))
if "Total time:" in line:
totaltime.append(float(line[47:57].strip()))
break
skip_lines = 2
offset = skip_lines-1
for line in islice(f, offset, None, skip_lines):
print(line)
totaltime = totaltime[0::9]
print(velerror)
print(mesh)
print(timestep)
print(totaltime)
打印每个文件的第 n 行
import glob
import numpy as np
import matplotlib.pyplot as plt
import math
velerror = []
divergence = []
mesh = []
timestep = []
totaltime = []
n_check = 5 # let n be 5
for files in glob.glob('*.out'):
f = open(files, 'r')
for i,line in enumerate(f):
if "Maximum Velocity Error:" in line:
velerror.append(float(line[32:40]))
if "Grid Dimensions, Mesh" in line:
mesh.append(int(line[27]))
if "Time Step " in line:
timestep.append(int(line[17:27].strip()))
if "Total time:" in line:
totaltime.append(float(line[47:57].strip()))
if not i%n_check:
print(line)
您可以使用关键字搜索特定行并打印第二个关键字的第n行。代码如下
search=open("test.txt",'r')
for line in search:
if "first_keyword" in line:
while not "second_keywoed" in line:
line=next(search)
if "second_keyword" in line:
print(line)
你可以为此使用 linecache 模块
import linecache
line_count = 0
n_lines = 4 # just an example
for file in glob.glob('*.out'):
with open(file, 'r') as f:
for line in f:
line_count += 1
if "Maximum Velocity Error:" in line:
velerror.append(float(line[32:40]))
if "Grid Dimensions, Mesh" in line:
mesh.append(int(line[27]))
if "Time Step " in line:
timestep.append(int(line[17:27].strip()))
if "Total time:" in line:
totaltime.append(float(line[47:57].strip()))
interesting_line = linecache.getline(file, line_count + n_lines)
print(interesting_line)
Is there a function like next() that goes to the next nth line?
有一个函数 next() 可以传递当前行并转到下一行。在包 itertools 中有一个函数 islice(),它构建了一个可迭代的子范围(即 next() 继续工作)。幸运的是,文件句柄(从 open() 调用中获得的 f )也是一个可迭代的,因此您可以将这三个部分组合起来:
from itertools import islice
...
if "Total time:" in line:
totaltime.append(float(line[47:57].strip()))
#Print nth line after line
# Skips to the 5th line, and stops and the 6th line;
# counting begins at 0, so by human standards this
# actually prints the 6th line:
next_5th_line = next (islice (f, 5, 6))
print (next_5th_line)
执行后,循环将在打印出的行之后继续,因此一些行将被简单地丢弃。
我正在尝试打印文件中特定行之后的第 n 行:
import glob
import numpy as np
import matplotlib.pyplot as plt
import math
velerror = []
divergence = []
mesh = []
timestep = []
totaltime = []
for files in glob.glob('*.out'):
f = open(files, 'r')
for line in f:
if "Maximum Velocity Error:" in line:
velerror.append(float(line[32:40]))
if "Grid Dimensions, Mesh" in line:
mesh.append(int(line[27]))
if "Time Step " in line:
timestep.append(int(line[17:27].strip()))
if "Total time:" in line:
totaltime.append(float(line[47:57].strip()))
#Print nth line after line
totaltime = totaltime[0::9]
print(velerror)
print(mesh)
print(timestep)
print(totaltime)
是否有像 next() 这样的函数可以转到下第 n 行?
使用readlines()
i = 0;
lines = f.readlines()
while 1:
line = lines[i]
if not line:
break
if "Maximum Velocity Error:" in line:
velerror.append(float(line[32:40]))
if "Grid Dimensions, Mesh" in line:
mesh.append(int(line[27]))
if "Time Step " in line:
timestep.append(int(line[17:27].strip()))
if "Total time:" in line:
totaltime.append(float(line[47:57].strip()))
i = i + 8
i++
(请参阅没有 readlines
、live demo 的版本的编辑)
print the nth line after a specific line in a file
如果文件适合内存使用 readlines
和切片:
def print_after(source, specific_line_number, skip_lines):
for line in source[specific_line_number::skip_lines]:
print(line)
lines = [f'line {i:2}' for i in range(30)] # readlines here
print_after(lines, 7, 2)
在您的代码中:
import glob
import numpy as np
import matplotlib.pyplot as plt
import math
velerror = []
divergence = []
mesh = []
timestep = []
totaltime = []
for files in glob.glob('*.out'):
f = open(files, 'r')
for line in f:
if "Maximum Velocity Error:" in line:
velerror.append(float(line[32:40]))
if "Grid Dimensions, Mesh" in line:
mesh.append(int(line[27]))
if "Time Step " in line:
timestep.append(int(line[17:27].strip()))
if "Total time:" in line:
totaltime.append(float(line[47:57].strip()))
for line in islice
break # Stop consuming lines (?)
totaltime = totaltime[0::9]
print(velerror)
print(mesh)
print(timestep)
print(totaltime)
编辑:按照 rassahah 的建议使用 islice 可以帮助您节省一些内存:
from itertools import islice
import glob
import numpy as np
import matplotlib.pyplot as plt
import math
velerror = []
divergence = []
mesh = []
timestep = []
totaltime = []
for files in glob.glob('*.out'):
f = open(files, 'r')
for line in f:
if "Maximum Velocity Error:" in line:
velerror.append(float(line[32:40]))
if "Grid Dimensions, Mesh" in line:
mesh.append(int(line[27]))
if "Time Step " in line:
timestep.append(int(line[17:27].strip()))
if "Total time:" in line:
totaltime.append(float(line[47:57].strip()))
break
skip_lines = 2
offset = skip_lines-1
for line in islice(f, offset, None, skip_lines):
print(line)
totaltime = totaltime[0::9]
print(velerror)
print(mesh)
print(timestep)
print(totaltime)
打印每个文件的第 n 行
import glob
import numpy as np
import matplotlib.pyplot as plt
import math
velerror = []
divergence = []
mesh = []
timestep = []
totaltime = []
n_check = 5 # let n be 5
for files in glob.glob('*.out'):
f = open(files, 'r')
for i,line in enumerate(f):
if "Maximum Velocity Error:" in line:
velerror.append(float(line[32:40]))
if "Grid Dimensions, Mesh" in line:
mesh.append(int(line[27]))
if "Time Step " in line:
timestep.append(int(line[17:27].strip()))
if "Total time:" in line:
totaltime.append(float(line[47:57].strip()))
if not i%n_check:
print(line)
您可以使用关键字搜索特定行并打印第二个关键字的第n行。代码如下
search=open("test.txt",'r')
for line in search:
if "first_keyword" in line:
while not "second_keywoed" in line:
line=next(search)
if "second_keyword" in line:
print(line)
你可以为此使用 linecache 模块
import linecache
line_count = 0
n_lines = 4 # just an example
for file in glob.glob('*.out'):
with open(file, 'r') as f:
for line in f:
line_count += 1
if "Maximum Velocity Error:" in line:
velerror.append(float(line[32:40]))
if "Grid Dimensions, Mesh" in line:
mesh.append(int(line[27]))
if "Time Step " in line:
timestep.append(int(line[17:27].strip()))
if "Total time:" in line:
totaltime.append(float(line[47:57].strip()))
interesting_line = linecache.getline(file, line_count + n_lines)
print(interesting_line)
Is there a function like next() that goes to the next nth line?
有一个函数 next() 可以传递当前行并转到下一行。在包 itertools 中有一个函数 islice(),它构建了一个可迭代的子范围(即 next() 继续工作)。幸运的是,文件句柄(从 open() 调用中获得的 f )也是一个可迭代的,因此您可以将这三个部分组合起来:
from itertools import islice
...
if "Total time:" in line:
totaltime.append(float(line[47:57].strip()))
#Print nth line after line
# Skips to the 5th line, and stops and the 6th line;
# counting begins at 0, so by human standards this
# actually prints the 6th line:
next_5th_line = next (islice (f, 5, 6))
print (next_5th_line)
执行后,循环将在打印出的行之后继续,因此一些行将被简单地丢弃。