我有一个包含原始数据的文本文件,但问题是文件中的所有内容都在一行中。我正在尝试用Python将其读入250X250阵列。我如何读取每个浮点令牌,以便我可以将每250个令牌存储在一个阵列中(然后将这250个阵列存储到另一个阵列中)?
以下是较大文本文件的示例:
2.07 2.07 2.07 2.07 2.07 2.07 2.07 2.07
答案 0 :(得分:4)
从文件生成数组然后重新整形的最简单方法。
>>> x=np.genfromtxt('X.txt')
>>> x
array([ 0.26027502, 0.52344602, 0.94682873, ..., 0.18915237,
0.12961926, 0.94742386])
>>> x.shape
(62500,)
>>> x = np.reshape(x, (250,250))
>>> x
array([[ 0.26027502, 0.52344602, 0.94682873, ..., 0.65515665,
0.97133803, 0.82415501],
[ 0.07134358, 0.71362599, 0.13989969, ..., 0.93441374,
0.68241895, 0.60303783],
[ 0.53491496, 0.39968921, 0.06248397, ..., 0.21473073,
0.16831451, 0.31789734],
...,
[ 0.10877778, 0.06762419, 0.81900428, ..., 0.74856093,
0.26248398, 0.22870396],
[ 0.37090122, 0.8382394 , 0.00154593, ..., 0.2041584 ,
0.46904135, 0.03799747],
[ 0.9419627 , 0.72610034, 0.44279619, ..., 0.18915237,
0.12961926, 0.94742386]])
>>> x.shape
(250, 250)
当然,您可以访问任何行或列....
>>> x[0,:]
array([ 0.26027502, 0.52344602, 0.94682873, 0.10665757, 0.79377438,
0.65801494, 0.58140499, 0.66640962, 0.43373581, 0.16128319,
0.70266644, 0.01818846, 0.01782449, 0.32032342, 0.97500891,
0.60327757, 0.59009519, 0.69106757, 0.94622942, 0.15136957,
0.1287025 , 0.19983005, 0.05298404, 0.92093835, 0.78505228,
0.83958482, 0.07733883, 0.62988452, 0.6814386 , 0.81682639,
0.03053231, 0.05236924, 0.40785548, 0.16167756, 0.44625781,
0.07929286, 0.21503215, 0.40479275, 0.45633735, 0.66524651,
0.90779273, 0.866891 , 0.94648979, 0.36298146, 0.93570324,
0.07487549, 0.90450725, 0.41796693, 0.58045668, 0.47335179,
0.6055467 , 0.49201359, 0.84480727, 0.81318106, 0.60810966,
0.23604474, 0.01072344, 0.11842985, 0.04188813, 0.36055357,
0.94984941, 0.03620454, 0.34074396, 0.95193302, 0.91658527,
0.73808885, 0.61518864, 0.43055782, 0.59904923, 0.14752041,
0.20079651, 0.65231914, 0.98935973, 0.21159892, 0.5232117 ,
0.96854742, 0.54765697, 0.74270461, 0.87702194, 0.86796058,
0.03775362, 0.93498823, 0.68149649, 0.2642807 , 0.37778898,
0.26517663, 0.58759482, 0.02787823, 0.49535491, 0.2782437 ,
0.98357993, 0.26340913, 0.24554617, 0.55307224, 0.22888235,
0.81380607, 0.66314271, 0.40915715, 0.02140317, 0.96048781,
0.82699845, 0.99421736, 0.25717857, 0.69675432, 0.4508669 ,
0.71091636, 0.36017548, 0.2318293 , 0.76682321, 0.73577509,
0.89874027, 0.0409444 , 0.16536175, 0.44526221, 0.60660233,
0.45460512, 0.98345256, 0.3663255 , 0.41726855, 0.96165547,
0.64764743, 0.47367676, 0.85408071, 0.93389485, 0.31320421,
0.40817909, 0.44824983, 0.405523 , 0.21725864, 0.9549355 ,
0.43012559, 0.69112666, 0.11570064, 0.09021077, 0.45238228,
0.73020711, 0.96987082, 0.70582309, 0.2553982 , 0.44727506,
0.34449858, 0.76815233, 0.83751986, 0.85879128, 0.36974847,
0.60779081, 0.38851576, 0.75236873, 0.02096765, 0.49170901,
0.22284193, 0.24787976, 0.45656373, 0.25239485, 0.7795917 ,
0.12916153, 0.48706096, 0.86036065, 0.51849597, 0.93765905,
0.25232495, 0.62851966, 0.95089984, 0.63946114, 0.46297102,
0.35535271, 0.58080051, 0.94821943, 0.9492573 , 0.68737527,
0.39894097, 0.6795753 , 0.43546261, 0.82748096, 0.40663179,
0.4553727 , 0.48005262, 0.82583658, 0.99992761, 0.22148 ,
0.65097962, 0.2815746 , 0.17627066, 0.44245061, 0.26597802,
0.38617241, 0.24852152, 0.50053587, 0.08652393, 0.84219587,
0.39225673, 0.26653707, 0.54875435, 0.82991054, 0.89961175,
0.41999139, 0.47578158, 0.72916822, 0.67832111, 0.35396472,
0.29938162, 0.91833388, 0.37599509, 0.64598379, 0.46118518,
0.82788677, 0.58454944, 0.09946118, 0.69875907, 0.69998759,
0.3966504 , 0.99769481, 0.80346738, 0.81667523, 0.21152416,
0.28419954, 0.17771031, 0.23944737, 0.74009348, 0.22675329,
0.37958291, 0.48378594, 0.77022399, 0.2577831 , 0.84859331,
0.76122995, 0.48038794, 0.80287582, 0.74552124, 0.38759297,
0.65152917, 0.98425492, 0.14493465, 0.96483483, 0.6265678 ,
0.1625753 , 0.66836669, 0.61827808, 0.12794944, 0.8109776 ,
0.6032338 , 0.88499851, 0.59360774, 0.57160687, 0.22021264,
0.39861853, 0.82982888, 0.65515665, 0.97133803, 0.82415501])
>>>
答案 1 :(得分:0)
您可以使用正则表达式模块重新
import re
lineArray = map(float, filter(None, re.split("\s", line)))
lineArray
然后包含所有值为float。
一些背景知识:split()中的参数"%s"
告诉例程在每个空格(tab,linebreak,space ..)中划分字符串,filter()
取出空字符串,因为它们会当找到两个连续的空格并且map(float, list)
将float()应用于列表的所有元素时发生。
答案 2 :(得分:0)
首先使用正则表达式转换为浮点数组,然后将数组切片为数组内的250个数组。
import re
one_line = map(float, filter(None, re.split("\s", s)))
def slice_per(array, slice_size):
return [array[i::slice_size] for i in range(slice_size)]
arrayed_pic = slice_per(one_line, 250)
答案 3 :(得分:0)
values = "2.07 2.07 2.07 2.07 2.07 2.07 2.07 2.07 2.07 2.07 2.07 2.07 2.07 2.07 2.07 2.07 2.07 2.07 2.07 2.07 2.07 2.07 2.07 2.07"
list_of_flt_values = values.split()
array_of_list = []
size_of_each_list = 8
leng_of_flt_values = len(list_of_flt_values)
if size_of_each_list % 2 == 0 :
number_of_list_blocks = int(leng_of_flt_values / size_of_each_list)
else:
number_of_list_blocks = int(leng_of_flt_values / size_of_each_list) + 1
for i in range(0, number_of_list_blocks):
if (i + 1) * size_of_each_list > leng_of_flt_values:
array_of_list.append(list(list_of_flt_values[i*size_of_each_list:]))
else:
array_of_list.append(list(list_of_flt_values[i*size_of_each_list:(i+1)*size_of_each_list]))
print array_of_list
我喜欢rth如何处理这个问题,如果你想从头开始做,你可以试试这个
答案 4 :(得分:0)
您可以使用列表推导拆分字符串并转换为浮点数,然后使用range
生成将其拆分为最终数组的指标。我将演示10x10阵列
>>> text = " ".join(str(float(i)) for i in range(100))
>>> floats = [float(s) for s in text.split(' ')]
>>> array = [floats[i:i+10] for i in range(0,100,10)]
>>> for l in array:
... print(l)
...
[0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
[10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0]
[20.0, 21.0, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0]
[30.0, 31.0, 32.0, 33.0, 34.0, 35.0, 36.0, 37.0, 38.0, 39.0]
[40.0, 41.0, 42.0, 43.0, 44.0, 45.0, 46.0, 47.0, 48.0, 49.0]
[50.0, 51.0, 52.0, 53.0, 54.0, 55.0, 56.0, 57.0, 58.0, 59.0]
[60.0, 61.0, 62.0, 63.0, 64.0, 65.0, 66.0, 67.0, 68.0, 69.0]
[70.0, 71.0, 72.0, 73.0, 74.0, 75.0, 76.0, 77.0, 78.0, 79.0]
[80.0, 81.0, 82.0, 83.0, 84.0, 85.0, 86.0, 87.0, 88.0, 89.0]
[90.0, 91.0, 92.0, 93.0, 94.0, 95.0, 96.0, 97.0, 98.0, 99.0]