从文件中导出数据

时间:2013-11-18 03:24:29

标签: python data-structures file-io

我刚刚开始使用Python。我有一些fortran和一些Matlab技能​​,但我绝不是一个编码器。我需要后处理一些输出文件。

我无法弄清楚如何将每个值读入相应的变量。数据看起来像这样:

  

h5097600N1 2348.13 2348.35 -0.2219 20.0 -4.438
   h5443200N1 2348.12 2348.36 -0.2326 20.0 -4.651
   h8467200N2 2348.11 2348.39 -0.2813 20.0 -5.627   ...

在我有限的Matlab表示法中,我想分配以下形式为tN1(i,j)的变量:

tN1(1,1)=5097600; tN1(1,2)=5443200; tN2(1,3)=8467200; #time between 'h' and 'N#'
hmN1(1,1)=2348.13; hmN1(1,2)=2348.12; hmN2(1,3)=2348.11; #value in 2nd column
hsN1(1,1)=2348.35; hsN1(1,2)=2348.36; hsN2(1,3)=2348.39; #value in 3rd column

我将有大约30套,或tN1(1:30,1:j); hmN1(1:30,1:j)的; hsN1(1:30,1:j)的

我知道它可能看起来不像,但我现在已经试图解决这个问题2天了。我试图自己学习这个,似乎我在理解python时缺少一些基本的东西。

2 个答案:

答案 0 :(得分:0)

我写了一个简单的脚本来完成你所要求的。它创建了三个词典t, hmhs。这些将具有键作为N值。

import csv
import re


path = 'vector_data.txt'

# Using the <with func as obj> syntax handles the closing of the file for you.
with open(path) as in_file:
    # Use the csv package to read csv files
    csv_reader = csv.reader(in_file, delimiter=' ')
    # Create empty dictionaries to store the values
    t = dict()
    hm = dict()
    hs = dict()
    # Iterate over all rows
    for row in csv_reader:
        # Get the <n> and <t_i>  values by using regular expressions, only
        # save the integer part (hence [1:] and [1:-1])
        n = int(re.findall('N[0-9]+', row[0])[0][1:])
        t_i = int(re.findall('h.+N', row[0])[0][1:-1])

        # Cast the other values to float
        hm_i = float(row[1])
        hs_i = float(row[2])

        # Try to append the values to an existing list in the dictionaries.
        # If that fails, new lists is added to the dictionaries.
        try:
            t[n].append(t_i)
            hm[n].append(hm_i)
            hs[n].append(hs_i)
        except KeyError:
            t[n] = [t_i]
            hm[n] = [hm_i]
            hs[n] = [hs_i]

输出:

>> t
{1: [5097600, 5443200], 2: [8467200]}
>> hm
{1: [2348.13, 2348.12], 2: [2348.11]}
>> hn
{1: [2348.35, 2348.36], 2: [2348.39]}

(请记住,Python使用零索引)

答案 1 :(得分:0)

感谢您的所有评论。建议的读数导致了其他有帮助的事情。以下是我提出的建议:

if len(line) >= 45:
    if line[0:45] == " FIT OF SIMULATED EQUIVALENTS TO OBSERVATIONS": #! indicates data to follow, after 4 lines of junk text
         for i in range (0,4):
             junk = file.readline()
         for i in range (0,int(nobs)):
             line = file.readline()
             sline = line.split()
             obsname.append(sline[0])
             hm.append(sline[1])
             hs.append(sline[2])