我是python的新手,虽然我确信这可能是一个微不足道的问题,但我花了一天时间试图以不同的方式解决这个问题。我有一个包含如下数据的文件:
<string>
<integer>
<N1>
<N2>
data
data
...
<string>
<integer>
<N3>
<N4>
data
data
...
这延伸了很多次......我需要读取“数据”,第一组(第一组和第二组之间)包含一个数字N1的X点,一个数字N2的Y点和一个数字Z点的N1 * N2。如果我只有一组数据,我已经知道如何读取所有数据,然后读取值N1,N2,然后将其切成X,Y和Z,重新整形并使用它......但如果我的文件包含更多如果不是一组数据,我如何只从一个字符串读取到下一个字符串,然后对下一个字符串重复相同的操作,并再次直到我到达文件的末尾? 我尝试定义一个函数:
def dat_fun():
with open("inpfile.txt", "r") as ifile:
for line in ifile:
if isinstance('line', str) or (not line):
break
for line in ifile:
yield line
但是没有用,我得到没有数据的数组。任何意见将不胜感激。 谢谢!
答案 0 :(得分:7)
所有行都是str
的实例,因此您在第一行突破。删除该测试,并首先通过剥离空白来测试空行:
def dat_fun():
with open("inpfile.txt", "r") as ifile:
for line in ifile:
if not line.strip():
break
yield line
我认为你不需要打破空行,真的; for
循环在文件末尾自行结束。
如果您的行包含其他类型的数据,则您需要自己进行转换,从 字符串进行转换。
答案 1 :(得分:3)
对于这样的结构化数据,我建议只阅读你需要的东西。例如:
with open("inpfile.txt", "r") as ifile:
first_string = ifile.readline().strip() # Is this the name of the data set?
first_integer = int(ifile.readline()) # You haven't told us what this is, either
n_one = int(ifile.readline())
n_two = int(ifile.readline())
x_vals = []
y_vals = []
z_vals = []
for index in range(n_one):
x_vals.append(ifile.readline().strip())
for index in range(n_two):
y_vals.append(ifile.readline().strip())
for index in range(n_one*n_two):
z_vals.append(ifile.readline().strip())
您可以通过添加循环并生成值来将其转换为数据集生成函数:
with open("inpfile.txt", "r") as ifile:
while True:
first_string = ifile.readline().strip() # Is this the name of the data set?
if first_string == '':
break
first_integer = int(ifile.readline()) # You haven't told us what this is, either
n_one = int(ifile.readline())
n_two = int(ifile.readline())
x_vals = []
y_vals = []
z_vals = []
for index in range(n_one):
x_vals.append(ifile.readline().strip())
for index in range(n_two):
y_vals.append(ifile.readline().strip())
for index in range(n_one*n_two):
z_vals.append(ifile.readline().strip())
yield (x_vals, y_vals, z_vals) # and the first string and integer if you need those
答案 2 :(得分:1)
def dat_fun():
with open("inpfile.txt", "r") as ifile:
for line in ifile:
if isinstance('line', str) or (not line): # 'line' is always a str, and so is the line itself
break
for line in ifile:
yield line
将其更改为:
def dat_fun():
with open("inpfile.txt", "r") as ifile:
for line in ifile:
if not line:
break
yield line