2014-01-10 76 views
1

这一直在杀死我整天/晚,我似乎无法想出一个解决方案。基本上,我有一个文本文件,其中包含一个二维矢量(由C++程序生成)在双打中。我需要在Python中将它读入二维数组,以便绘制谱图。这里是数据的样子:从文本文件读取块到二维数组

-18.2258 -18.3581 -18.7323 -19.2183 -19.8016 -20.6132 -21.8101 -22.5386 -21.8071  
-20.9063 -20.4136 -20.3022 -20.3428 -20.4091 -20.6703 -21.0293 -21.5167 -22.1915  
-23.0438 -23.9086 -24.5955 -26.2508 -26.0188 -22.2163 -19.933 -18.6816 -18.1048 
-18.0222 18.3233 -19.0456 -20.3134 -22.7954 -25.8716 -21.4845 -19.1923 -17.9268 
-17.4657 -17.3888 -16.9999 -16.4006 -15.9175 -15.8319 -16.1705 -16.6967 -17.0734 


-7.92685 -10.8266 -16.392 -12.4901 -13.0831 -17.7215 -17.5159 -14.1485 -12.9897 -12.0444 
-11.8363 -12.6952 -12.9652 -14.3788 -13.8465 -17.529 -17.4747 -11.9521 -12.545 -13.8976 
-12.4176 -15.3273 -14.8081 -19.4117 -17.9596 -16.2607 -16.7505 -15.8918 -16.5602 
-17.2225 -16.9048 -15.1381 -17.37 -16.43 -14.9437 -14.9821 

每个数据块在文本文件中用2行分隔。

我曾尝试以下:

with open('spec.txt') as file: 
    array2d = [[float(digit) for digit in line.split()] for line in file] 

但是,这并不工作,我只是似乎得到了很多产生的阵列。

任何人有任何想法来解决这个问题?

P.S.每个块的大小相同。但是,为了缩短这个问题,我只包含了一个示例。

+0

你用什么来绘图? “2d数组”究竟是什么意思?列表清单?或者是一个'numpy'数组? – hpaulj

+0

@hpaulj嘿,我使用matplotlib来绘制..我试过了Ashwini Chaudhary的实现,但是,它不起作用。结果有点搞砸 – Phorce

+0

如果你的第二个块的数字与第一个块的数量相同,那么测试起来会更容易。这样,列表的结果列表可以直接输入到'numpy.array'来创建一个2d数组。 – hpaulj

回答

2
raw_text = """-18.2258 -18.3581 -18.7323 -19.2183 -19.8016 -20.6132 -21.8101 -22.5386 -21.8071  
-20.9063 -20.4136 -20.3022 -20.3428 -20.4091 -20.6703 -21.0293 -21.5167 -22.1915  
-23.0438 -23.9086 -24.5955 -26.2508 -26.0188 -22.2163 -19.933 -18.6816 -18.1048 
-18.0222 18.3233 -19.0456 -20.3134 -22.7954 -25.8716 -21.4845 -19.1923 -17.9268 
-17.4657 -17.3888 -16.9999 -16.4006 -15.9175 -15.8319 -16.1705 -16.6967 -17.0734 


-7.92685 -10.8266 -16.392 -12.4901 -13.0831 -17.7215 -17.5159 -14.1485 -12.9897 -12.0444 
-11.8363 -12.6952 -12.9652 -14.3788 -13.8465 -17.529 -17.4747 -11.9521 -12.545 -13.8976 
-12.4176 -15.3273 -14.8081 -19.4117 -17.9596 -16.2607 -16.7505 -15.8918 -16.5602 
-17.2225 -16.9048 -15.1381 -17.37 -16.43 -14.9437 -14.9821""" 
#in your example raw_text = open(some_file).read() 
blocks = raw_text.split("\n\n\n") 
split_blicks = [[float(v) for v in block.split()] for block in blocks] 

是你想要的吗?

+0

是的,但是,它是否必须进行硬编码? – Phorce

+0

什么?没有看到注释行 –

0

斯普利特在空行数据:

def split_at_empty_lines(filename): 
    with open(filename) as f: 
     arr = [] 
     for line in f: 
      #If the line is empty and arr is not empty, means it's 
      #time to return the collected items and set `arr` back to []. 
      if not line.strip() and arr: 
       yield arr 
       arr = [] 
      #If the line is not empty then simply collect the items in `arr` 
      elif line.strip(): 
       arr.extend(float(x) for x in line.split()) 
      #Ignore the case of empty line and empty `arr` 

     #Check if arr is not empty or not, if not empty returns its content. 
     if arr: yield arr 
...   
>>> list(split_at_empty_lines('abc1.txt')) 
[ 
[-18.2258, -18.3581, -18.7323, -19.2183, -19.8016, -20.6132, -21.8101, -22.5386, -21.8071, -20.9063, -20.4136, -20.3022, -20.3428, -20.4091, -20.6703, -21.0293, -21.5167, -22.1915, -23.0438, -23.9086, -24.5955, -26.2508, -26.0188, -22.2163, -19.933, -18.6816, -18.1048, -18.0222, 18.3233, -19.0456, -20.3134, -22.7954, -25.8716, -21.4845, -19.1923, -17.9268, -17.4657, -17.3888, -16.9999, -16.4006, -15.9175, -15.8319, -16.1705, -16.6967, -17.0734], 
[-7.92685, -10.8266, -16.392, -12.4901, -13.0831, -17.7215, -17.5159, -14.1485, -12.9897, -12.0444, -11.8363, -12.6952, -12.9652, -14.3788, -13.8465, -17.529, -17.4747, -11.9521, -12.545, -13.8976, -12.4176, -15.3273, -14.8081, -19.4117, -17.9596, -16.2607, -16.7505, -15.8918, -16.5602, -17.2225, -16.9048, -15.1381, -17.37, -16.43, -14.9437, -14.9821] 
] 
+0

谢谢!但是,我怎么能将这个方法的结果存储在另一个数组中,以便它可以被绘制? – Phorce

+0

@ user1326876我不明白,简单地调用它的list()(如代码所示),你会得到一个列表清单。 –

0

你可以颇有几分与列表内涵酌减。

with open('myfile') as f: 
    return ([float(x) for x in l.split() if l] for l in (raw.strip() for raw in f)) 

请注意,外部parens使得此返回一个生成器,而不是在返回任何内容之前处理整个文件。

相关问题