简体   繁体   English

如何将文本文件更改为二维numpy数组?

[英]How to change a text file into a two dimensional numpy array?

I wanted to change a text file into a two-dimensional numpy array. 我想将文本文件更改为二维numpy数组。 my text file looks like this if opened. 如果打开,我的文本文件将如下所示。

01110111110111010111011101110111010111011111010101110101011101110
01000001000001010001010100010101010000010001010100010101000100010
01110111011111011101010111011101110111110111010111010101110111010
01010000010000010101010001010000000100000000010101010000010101010
01010111110111110101010101010111111101110111110101010111110101010

later I want to access every element of the line. 稍后,我想访问该行的每个元素。 I appreciate anyone with hints: 我感谢任何有提示的人:

Just do: 做就是了:

with open(file_path, 'rt') as f:
    result = np.array([[ int(c) for c in line ] for line in f ])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM