简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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