1

I have a text file filled with lines like this:

[[19.305199999999999, -126.56959999999999], [19.445499999999999, -126.46599999999999], [19.196999999999999, -126.396], [19.130700000000001, -126.65900000000001], [19.378900000000002, -126.73]]

When I read these lines, they are interpreted as strings. Is there any way I can read them as lists or arrays or easily convert them into lists or arrays?

Barmar
  • 596,455
  • 48
  • 393
  • 495
Ryan Clare
  • 21
  • 3

1 Answers1

0

Use the ast module

Ex:

import ast
l = "[[19.305199999999999, -126.56959999999999], [19.445499999999999, -126.46599999999999], [19.196999999999999, -126.396], [19.130700000000001, -126.65900000000001], [19.378900000000002, -126.73]]"
print(ast.literal_eval(l))

Output:

[[19.3052, -126.5696], [19.4455, -126.466], [19.197, -126.396], [19.1307, -126.659], [19.3789, -126.73]]
Rakesh
  • 75,210
  • 17
  • 57
  • 95