Reputation: 173
I have a text file coords.txt containing lists in separated rows:
[0, 0.0, 0.0]
[0, 2.0, 1.0]
[0, 1.0, 4.0]
the first column is ID, second and third ones - coordinates.
I want to obtain following effect:
[[0, 0.0, 0.0], [0, 2.0, 1.0], [0, 1.0, 4.0]]
I try the code:
file = open(coords.txt).read()
list = file.splitlines()
Result is:
['[0, 0.0, 0.0]', '[0, 2.0, 1.0]', '[0, 1.0, 4.0]']
The other way I used is:
file = open(cords.txt)
list= [line.translate(None, '[]').split('\n') for line in file.readlines()]
Result is:
[['0, 0.0, 0.0', ''], ['0, 2.0, 1.0', ''], ['0, 1.0, 4.0']]
How can I obtain expected outcome (get into the list to change strings to floats)?
Upvotes: 2
Views: 83
Reputation: 180441
If you don't mind having all floats, you can strip the []
and newline then split on commas mapping to float:
with open("in.txt") as f:
data = [list(map(float, line.strip("[]\n").split(","))) for line in f]
print(data)
[[0.0, 0.0, 0.0], [0.0, 2.0, 1.0], [0.0, 1.0, 4.0]]
Or using re to find all the digits:
import re
with open("in.txt") as f:
r = re.compile("\d+\.\d+|\d+")
data = [list(map(float, r.findall(line)))for line in f]
print(data)
[[0.0, 0.0, 0.0], [0.0, 2.0, 1.0], [0.0, 1.0, 4.0]]
Using translate you would split on ,
after removing the []
:
data = [list(map(float, line.rstrip().translate(None,"[]").split(","))) for line in f]
Unrelated but you don't need to call readlines, you can just iterate over the file object instead of creating a list with readlines unnecessarily.
Upvotes: 1
Reputation: 239483
Since your data is in the same format as when a list of floats is printed as a string, you can use ast.literal_eval
method to convert it back to the actual list, like this
>>> import ast
>>> with open("coords.txt") as input_file:
... result = [ast.literal_eval(line) for line in input_file]
...
>>> result
[[0, 0.0, 0.0], [0, 2.0, 1.0], [0, 1.0, 4.0]]
We open the file with the with
statement, and then read the lines in the file one by one with the for
loop (for line in input_file
). We do ast.literal_eval
on the read lines and the result of the call is stored in the list, with the list comprehension. You can read more about the with statement, here, here and here.
Actual problems with your approaches are,
file = open(coords.txt).read()
list = file.splitlines()
will just read the entire contents as a single string and then just split the string on the new line characters. But the list will still have only strings.
And when you tried
file = open(cords.txt)
list= [line.translate(None, '[]').split('\n') for line in file.readlines()]
you have just removed the []
characters from the individual lines, and split the line based on \n
. You still haven't attempted to convert them back to floats. It will still be string only.
Upvotes: 2
Reputation: 22954
For each line in the text file, you first need to get rid of the square braces, then you split the string on ", "
(mid the space here), Then after splitting you get a list of strings then you need to convert those strings to floats, for which you can use map()
function.
data = []
with open("sample.txt", "r") as data_file:
for line in data_file.readlines():
line = line.strip()[1:-1] #To remove the starting and ending braces
data.append(map(float, line.split(", ")))
print data
Upvotes: 1