# create a `bytes` object from a `str`
my_str = "Hello,\\nworld"
# (pick an encoding suitable for your str, e.g. 'latin1')
my_bytes = my_str.encode("utf-8")
# or directly
my_bytes = b"Hello,\\nworld"
print(my_bytes.decode("unicode_escape"))
# "Hello,
# world"
Safely evaluate an expression node or a string containing a Python
expression. The string or node provided may only consist of the
following Python literal structures: strings, numbers, tuples, lists,
dicts, booleans, and None. (END)
在python 3中,
str
对象没有decode
方法,必须使用bytes
对象。克里斯托弗的答案涵盖了python 2。您可以使用
ast.literal_eval
这是安全的:像这样:
相关问题 更多 >
编程相关推荐