1

This seems like a simple question, but couldn't find it on the Stack community. I have a dataset like the one below in a text file. I would like to read it in as a list with each value as a float. Currently the output is very odd from the simple list needed (also below).

data.txt:

[1130.1271455966723, 1363.3947962724474, 784.433380329118, 847.2140341725295, 803.0276763894814,..]

Code attempted:

my_file = open(r"data.txt", "r")
content = my_file.read()
content_list = content.split(",")
my_file.close()

The output is odd. The values are string and list inside of list and added spaces:

Current result:

['[1130.1271455966723',
 ' 1363.3947962724474',
 ' 784.433380329118',
 ' 847.2140341725295',
 ' 803.0276763894814',
 ' 913.7751118925291',
 ' 1055.3775618432019',...]']

I also tried the approach here (How to convert string representation of list to a list?) with the following code but produced an error:

import ast
x = ast.literal_eval(result)
raise ValueError('malformed node or string: ' + repr(node))
ValueError: malformed node or string: ['[1130.1271455966723', '1363.3947962724474', ' 784.433380329118', ' 847.2140341725295', ' 803.0276763894814',...]']

Ideal result:

list = [1130.1271455966723, 1363.3947962724474, 784.433380329118, 847.2140341725295, 803.0276763894814]
martineau
  • 119,623
  • 25
  • 170
  • 301
maximus
  • 335
  • 2
  • 16
  • Why did you store this like this to begin with? Note, it *happens* to be valid JSON, so you could just treat it as JSON. But you should *use something like JSON or `pickle`* to serialize your data. Don't just dump the string representation to a file. – juanpa.arrivillaga Sep 22 '21 at 18:36
  • 1
    You're just splitting on commas, when the file has items separated by a comma and a space. You're not stripping off the brackets either. And you're not converting each item to a float. So, you have to ignore the brackets, split on the correct thing, and convert each item to a float. There's plenty of resources online for how to do each of those things. However storing it that way in the first place was unnecessary; if you stored each float on its own line in the text file, with no commas or brackets, that would simplify things quite a bit. – Random Davis Sep 22 '21 at 18:36
  • Note, *in general* for `some_object`, if you do `file.write(str(some_object))` there is *no guarantee* you can actually recover `some_object` from the text you created – juanpa.arrivillaga Sep 22 '21 at 18:37

1 Answers1

3

Your data is valid JSON, so just use the corresponding module that will take care of all the parsing for you:

import json

with open("data.txt") as f:
    data = json.load(f)

print(data)

Output:

[1130.1271455966723, 1363.3947962724474, 784.433380329118, 847.2140341725295, 803.0276763894814]
Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50