Array of Python load from ascii .txt file.
There is a .txt file named "array.txt" which contains the following 5x5 array of two-digit numbers:
+-------------------------+
¦ 34 ¦ 21 ¦ 32 ¦ 41 ¦ 25 ¦
+----+----+----+----+-----¦
¦ 14 ¦ 42 ¦ 43 ¦ 14 ¦ 31 ¦
+----+----+----+----+-----¦
¦ 54 ¦ 45 ¦ 52 ¦ 42 ¦ 23 ¦
+----+----+----+----+-----¦
¦ 33 ¦ 15 ¦ 51 ¦ 31 ¦ 35 ¦
+----+----+----+----+-----¦
¦ 21 ¦ 52 ¦ 33 ¦ 13 ¦ 23 ¦
+-------------------------+
I want a script that reads this file automatically without manually specifying:
array = np.matrix([[34,21,32,41,25],[14,42,43,14,31],[54,45,52,42,23],[33,15,51,31,35],[21,52,33,13,23]])
All I have is this:
import numpy as np
np.loadtxt('array.txt', skiprows=1)
which returns the error "ValueError: cannot convert string to float: b'xa6". So it seems like he doesn't like ascii characters. Is there a function that can only read the numeric values of a text file into an array? Thanks so much for reading, any help would be infinitely appreciated.
source to share
You can use regex to extract numbers from strings:
import re, numpy
with open(myFile, 'r') as content:
# This extracts all word boundary-delimited numbers from each line
x = [re.findall(r'\b\d+\b', i) for i in content.readlines()]
# Then you keep only those lines that contained a number and
# convert the resulting list to an array
myArray = numpy.array([i for i in x if len(i) > 0])
source to share
Assuming these are the only delimiters you'll ever run into your .txt file, here's a little I know in Python 2.7.
array = []
for line in open('array.txt', 'r').readlines():
if line.startswith('\xc2'):
line = line.replace('\xa6','').replace('\xc2',',').split(',')
line[:] = [int(x) for x in line if x not in ['','\n']]
array.append(line)
Calling additional print array
in the test text file you gave gives this output:
[[34, 21, 32, 41, 25], [14, 42, 43, 14, 31], [54, 45, 52, 42, 23], [33, 15, 51, 31, 35], [21, 52, 33, 13, 23]]
Here you can customize it to suit your numpy needs.
source to share