1

I am reading lines from file in a list:

import numpy as np

lines = tuple(open('values.txt','r'))
x = np.array([line for line in lines])

values.txt looks like:

[1,0,1,0],
[1,0,0,0]

It throws an error:

valueError: invalid literal for float()

However, if I just assign the list to x, it works just fine.

How to take input from file in a numpy array?

1
  • 1
    line from the file is a string, not a list. See what you get when float('[1,0]'). Commented Jan 29, 2016 at 0:25

3 Answers 3

1
lines = open('values.txt', 'r')
x = np.array( [  map(float, (l[l.find("[")+1 : l.find("]")].split(",")))
     for l in lines ] )
print x

A brief explanation:

This takes each line in your file, finds the brackets on each side, and takes the string within the brackets. We then split that string into an array using commas as the delimiter. Then, we have an array of strings so we map the float function onto each element, turning it into a floating point number. Then we use the standard list comprehension to do this to every line.

Sign up to request clarification or add additional context in comments.

1 Comment

If you specify dtype=float, np.array will take care of the map(float...) step: np.array([l[l.find("[")+1 : l.find("]")].split(",") for l in txt ], dtype=float)
1
import numpy as np
import ast

lines = open('values.txt','r')
x = np.array([ast.literal_eval(line.strip(',\n')) for line in lines])

4 Comments

Building an entire abstract syntax tree for each line for this seems unnecessarily expensive computationally, but to each their own.
I'm seeing no such slowdown with 1 million lines, if anything mine appears to be slightly faster in my tests, aside from the extra import
Shrug. It was only speculation on my part. Would be curious to see your results, as I have a hard time believing that it's faster beyond the critical value of statistical significance.
The basis of my speculation was incorrect. I had looked up Generalized LR parsing, and saw that the complexity of GLR was O(n^3). I mistakenly assumed that more efficient parsers (i.e. those used to parse code for compilers) had a time complexity greater than O(n) -- this was mistaken. LALR, LR(1) and LR(k) parsing have time complexity O(n), which is the complexity of my "algorithm" as well.
0

You can read the whole file at once and just add the outer [] before applying literal_eval() to all lines:

from ast import literal_eval

with open('values.txt') as fobj:
    x = np.array(literal_eval('[{}]'.format(fobj.read())))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.