0

Right now I have a dataset where each line is a string in the form:

"[6.43736, 23, {}, [], '']"

I want to convert it into a list, but i cant for the life of me figure out of to do it. The preferred output would look like this:

[6.43736, 23, {}, [], '']

For each line in the csv file.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Bogehave
  • 13
  • 2
  • 1
    "Right now i have a dataset where each line is a string in the form:" What generated such a string? Somewhere, someone decided "let me save this string representation of a Python data structure in a csv file and pretend it is serialization". It isn't. Use a pre-built serialization format, like JSON – juanpa.arrivillaga Feb 28 '19 at 19:06

1 Answers1

3

ast.literal_eval is good for safely evaluating strings that look like numbers, tuples, lists, dicts, bools, None, and strings:

>>> s = "[6.43736, 23, {}, [], '']"
>>> import ast
>>> ast.literal_eval(s)
[6.43736, 23, {}, [], '']
Kevin
  • 74,910
  • 12
  • 133
  • 166
  • Thank you for such a quick reply! However, this does not change the string to a list, which means I still can't index it. – Bogehave Feb 28 '19 at 19:05
  • @Bogehave yes, *it does if that string is exactly what you posted*. – juanpa.arrivillaga Feb 28 '19 at 19:09
  • literal_eval _should_ return a list in this case, which can be indexed. Are you assigning the result to anything? It won't alter `s` in-place; you'll need to do something like `seq = ast.literal_eval(s); print(seq[0])`. – Kevin Feb 28 '19 at 19:10
  • My bad, i think it may be an issue that im trying to do within a csv file? When i run this code i get for each output: `def sprog_csv(csv_file): with open(csv_file) as f: for item in f: seq = ast.literal_eval(item) print(type(seq))` – Bogehave Feb 28 '19 at 19:32
  • Hard to say without seeing the structure of the csv file. Wild guess: Maybe the string has an additional layer of quote marks. try `seq = ast.literal_eval(ast.literal_eval(item)))` – Kevin Feb 28 '19 at 19:37
  • You are 100% correct, thank you so much for the help Kevin! – Bogehave Mar 01 '19 at 07:32