This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 5 months ago.
I have message which is a list and JSON in it. I managed to get JSON with raw decode. The problem is
string = "["a,b","b,c"]"
How can I convert that string into a list?
Use ast.literal_eval:
import ast
print(ast.literal_eval(string))
Since this is JSON, use the json module:
import json
print(json.loads(string))
string = "['a,b','b,c']"
print(eval(string))
Related
This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 3 years ago.
I have a form that save lists as strings, I want to convert those strings to lists before save in DB.
I'm using python3 and Django.
The strings looks like:
'["MEX","MTY","GDL","BJX","QRO","PBC","TLC","CUN","EM","CUU"]'
what will be the most pythonic way to do that?
import json
json.loads('["MEX","MTY","GDL","BJX","QRO","PBC","TLC","CUN","EM","CUU"]')
This question already has answers here:
Removing u in list
(8 answers)
Closed 3 years ago.
I have a list of id's and I am trying the following below:
final = "ids: {}".format(tuple(id_list))
For some reason I am getting the following:
"ids: (u'213231231', u'weqewqqwe')
Could anyone help out on why the u is coming inside my final string. When I am trying the same in another environment, I get the output without the u''. Any specific reason for this?
Actually it is unicode strings in python
for literal value of string you can fist map with str
>>> final = "ids: {}".format(tuple(map(str, id_list)))
>>> final
"ids: ('213231231', 'weqewqqwe')
This question already has answers here:
How do I url unencode in Python?
(3 answers)
Closed 5 years ago.
I'm trying to find a python package/sample code that can convert the following input "why+don%27t+you+want+to+talk+to+me" to "why+don't+you+want+to+talk+to+me".
Converting the Hex codes like %27 to ' respectively. I can hardcode the who hex character set and then swap them with their symbols. However, I want a simple and scalable solution.
Thanks for helping
You can use urllib's unquote function.
import urllib.parse
urllib.parse.unquote('why+don%27t+you+want+to+talk+to+me')
This question already has answers here:
How to convert string representation of list to a list
(19 answers)
Closed 6 years ago.
I have a string, something like:
a = "[1, 2, 3]"
If there an easy way to convert it in to a list, without using .split(), join() etc.
Thanks for any replies.
use ast.literal_eval() it's safer than using eval
from ast import literal_eval
a = literal_eval("[1, 2, 3]")
print(a)
This question already has answers here:
Saving UTF-8 texts with json.dumps as UTF-8, not as a \u escape sequence
(12 answers)
Closed 7 months ago.
For example:
>>> print(json.dumps('růže'))
"r\u016f\u017ee"
(Of course, in the real program it's not just a single string, and it also appears like this in the file, when using json.dump()) I'd like it to output simply "růže" as well, how to do that?
Pass the ensure_ascii=False argument to json.dumps:
>>> print(json.dumps('růže', ensure_ascii=False))
"růže"