0

I have a List emp_id = "['1406','41232']" I want to convert the same into List ['1406','41232']

I tried

emp_id = emp_id.strip('][').split(", ")

But it resulted output was ["'1406'","'41232'"]"

kiran mathew
  • 1,882
  • 1
  • 3
  • 10
Ibrahim Khan
  • 184
  • 10
  • 1
    `ast.literal_eval(emp_id)`. – ShadowRanger Jun 03 '22 at 11:12
  • `eval(emp_id,{})` – codester_09 Jun 03 '22 at 11:13
  • 1
    @SharimIqbal: No. `eval` can execute arbitrary code, even when you hide the normal global namespace by passing `{}` (e.g. `eval('__import__("subprocess").run(["echo", "pwned"])', {})`). It's unsafe, and *unable to be made safe*; if you want to evaluate a Python literal, that's what `ast.literal_eval` is for. – ShadowRanger Jun 03 '22 at 11:19
  • @ShadowRanger `ast.literal_eval(emp_id)` worked. Thanks – Ibrahim Khan Jun 03 '22 at 11:21
  • @ShadowRanger Even this works `emp_id = emp_id.strip('][').split("', '")` `emp_id[0] = emp_id[0][1:len(emp_id[0])]` `emp_id[-1] = emp_id[-1][0:len(emp_id[-1])-1]` – Ibrahim Khan Jun 03 '22 at 11:23
  • if you would have values in `"` instead of `'` then you would have JSON data and you could use `emp_id = json.loads(emp_id)`. OR use `.replace()` to change it: `json.loads("['1406','41232']".replace("'", '"'))`. Eventually use module `dirtyjson` and it will work even with `'` - `dirtyjson.loads("['1406','41232']")` – furas Jun 03 '22 at 12:07
  • if you use `.replace()` to remove `'` then you should aslo get expected list - `emp_id.strip('][').replace("'", "").split(',')` – furas Jun 03 '22 at 12:11
  • @IbrahimKhan: That works for this *very* specific data. It's also needlessly complicated, making it a maintenance nightmare if *anything* about this data changes. – ShadowRanger Jun 03 '22 at 12:59
  • @furas: `dirtyjson` is a reasonable recommendation. Blanket replacing the quotes is a terrible idea; the moment the input data actually has interior quotes, everything goes to hell, and even when it works, it's still ugly, brittle hand-rolled parsing code that covers none of the edge cases (e.g. if it's got `repr` of `str`s that have backslash escapes that must be removed/replaced, a parser handles it, this doesn't). If you have a known format, with a useful parser, use the parser (that will cover all the little edge cases correctly), don't encourage reinventing the parser badly. – ShadowRanger Jun 03 '22 at 13:02
  • @ShadowRanger yes, replacing can make big problems but if OP can be sure that it always get similar data then using replace can be simpler. But I wondering how OP gets this data - if OP generates this string then it is should rather change code to generate correct `JSON` data :) – furas Jun 03 '22 at 13:34
  • @furas: `emp_id.strip('][').replace("'", "").split(',')` is simpler than `ast.literal_eval(emp_id)`? That's a new one on me. :-) – ShadowRanger Jun 03 '22 at 13:36
  • @ShadowRanger frankly, I didn't even think about `ast` :) Maybe because usually I have to work with data which can't be directly converted with `ast` because it needs to use `if/else` to select only some part of string. But in some questions I saw how people create complex code to parse data but it can be resolved with string functions like `replace`, `split`, etc. – furas Jun 03 '22 at 14:26

0 Answers0