这一直在整天杀死我,我似乎无法想出解决方案。基本上,我有一个包含2D矢量(从C ++程序生成)的文本文件。我需要在Python中将其读入2D数组中,以便绘制谱图。以下是数据的样子:
-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 :(得分: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]
是你想要的吗?
答案 1 :(得分: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]
]
答案 2 :(得分: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使得它返回一个生成器,而不是在返回任何内容之前处理整个文件。