+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

Python —逐行读取单个令牌

发布于2020-11-28 09:04     阅读(444)     评论(0)     点赞(3)     收藏(1)


0

1

2

3

4

我有一个包含原始数据的文本文件,但要注意的是它全部位于文件的一行中。我正在尝试使用Python将其读取到250X250数组中。我将如何读取每个浮点令牌,以便将每250个令牌存储在一个数组中(然后将这250个数组存储到另一个数组中)?

这是较大的文本文件的示例:

2.07 2.07 2.07 2.07 2.07 2.07 2.07 2.07 

解决方案


从文件生成数组然后进行整形的最简单方法。

>>> 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])
>>> 

0

1

2

3

4

5

6

7

8

9



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接: https://www.pythonheidong.com/blog/article/634468/4d78e27d1cb00c6e93a6/

来源: python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

3 0
收藏该文
已收藏

评论内容:(最多支持255个字符)