2013-07-02 42 views
2

我是python的新手,尽管我确信这可能是一个微不足道的问题,但我花了一天的时间试图以不同的方式解决这个问题。我有一个包含看起来像这样的数据文件:通过文本文件读取数据的Python循环

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

而且扩展了很多次......我需要阅读的“数据”,这对于首套(第一和第二之间)包含一个X点的数量N1,Y点的数量N2以及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

这是一个XML文件,如果是,你可以使用Python的内置XML解析模块。 – John

+0

它是一个纯文本文件。 @johnthexiii。我想要所有的套件,一些文件包含两套,还有一些。从每组数据中,我必须使用X,Y和Z来创建一些图,如果我只用一组“ \ n \ n \ n ”手动创建独立文件,数据....“每个文件。我希望能够读取一个集合,直到到达下一个(并使用其数据),然后读取下一组数据,直到达到下一个,依此类推直到文件结束。谢谢! – jealopez

回答

3

全部行都是str的实例,所以你在第一行中突然出现。首先剥离了空白删除测试,并测试一个空行:

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

我不认为你需要在一个空行突破,真的; for循环在文件末尾自行结束。

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

+0

因此,第一次测试以查看它的字符串是否不会区分来自“字母字符”字符串的浮点数(它是“数据”类型)还是整数(它们是N1,N2,N3 ...)?由于我的数据是数字,我想要获取数据数组,因此可以说数据1为第一个和第二个数据之间的数据,数据2为第二个和第三个数据之间的数据,直到文件结束为止。谢谢! – jealopez

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 
+0

'不行'不可能是'真';除最后一行之外的所有行都将换行,即使这样,最后一行也不会为空。 –

2

结构化数据是这样,我建议只阅读你所需要的。例如:

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 
+0

非常感谢!我认为如果我只对第一组数据感兴趣,这将是做这件事的方式,但是我想通过整个文件可以将每一组数据放入不同的数组中(让我们来说说数据到数据之间的数据)第一个和第二个,第二个和第三个之间的数据,等等,直到文件结束)。 “first_integer”是一个整数,它必须对产生该特定数据集的进程做更多的事情,所以我对那个不感兴趣,只在n_one和n_two ... – jealopez

+0

上,但我想一旦我明白了如何将字符串之间的数据放入数组中,我会更容易理解如何将整数n_one,n_two等等读取出来。谢谢。 – jealopez

+0

是的,第一个字符串(和每个字符串)是相应数据集的名称。 – jealopez