4

我是 python 新手,虽然我确信这可能是一个微不足道的问题,但我花了一天的时间试图以不同的方式解决这个问题。我有一个包含如下数据的文件:

<string>
<integer>
<N1>
<N2>
data
data
...
<string>
<integer>
<N3>
<N4>
data
data
...

并且延伸了很多次......我需要读取第一组(在第一组和第二组之间)的“数据”,其中包含 N1 个 X 点、一个 N2 个 Y 点和一个 N1*N2 Z 点。如果我只有一组数据,我已经知道如何读取所有数据,然后读取值 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

但不起作用,我得到没有数据的数组。任何意见将不胜感激。谢谢!

4

3 回答 3

7

所有行都是 的实例str,因此您在第一行突破。删除该测试,并通过首先去除空格来测试空行:

def dat_fun():
    with open("inpfile.txt", "r") as ifile:
        for line in ifile:
            if not line.strip():
                break
            yield line

我不认为你需要在空行上休息,真的;循环在for文件末尾自行结束。

如果您的行包含其他类型的数据,您需要自己进行转换,来自字符串。

于 2013-07-02T22:14:48.780 回答
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
于 2013-07-02T22:30:29.497 回答
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
于 2013-07-02T22:16:25.050 回答