Reputation: 6592
I have a txt containing both strings and floats (see below) and I need to load the data in a matrix. What is the best way to do it?
Dummy data row:
1 2 3.0 4.567 8.910 Data/file.txt
Below is my (non working) code. It doesn't return the first three variables, and it does't wrap.
import numpy as np
import scipy
matrix = []
with open('input.txt') as f:
for line in f:
el = line.split()
matrix.append(el[0] + el[1] + el[2] + el[3] + el[4] + el[5])
print matrix
Upvotes: 0
Views: 203
Reputation: 928
It's working for me. is the indentation of for loop is typo or not?
import numpy as np
import scipy
matrix = []
with open('input.txt') as f:
for line in f:
el = line.split()
matrix.append(el[0] + el[1] + el[2] + el[3] + el[4] + el[5])
print matrix
output: ['123.04.5678.910Data/file.txt']
Upvotes: 1
Reputation: 250941
You can use numpy.genfromtxt
with dtype=None
:
>>> import numpy as np
>>> np.genfromtxt('input.txt', dtype=None)
array((1, 2, 3.0, 4.567, 8.91, 'Data/file.txt'),
dtype=[('f0', '<i4'), ('f1', '<i4'), ('f2', '<f8'), ('f3', '<f8'), ('f4', '<f8'), ('f5', 'S13')])
Upvotes: 0
Reputation: 489
Are the number of entries the same for each row? If so, I'd recommend using pandas. Use the read_csv
function and specify the separator to be \s+
.
If you want it to be tabular, store it in a Data Frame.
Upvotes: 1