[英]Using python, How to replace single quotes to double quotes
I was giving input in double quotes, after processing some operations need to display output in double quotes where its giving in single quote format我在双引号中输入,在处理一些操作后需要在双引号中显示 output ,其中它以单引号格式给出
code:代码:
def movie_data(movie_list):
mve = {}
for k, v in movie_list.items():
if type(v) == str:
mve[k] = str(v) if v else None
elif type(v) == dict:
mve[k] = movie_data(v)
else:
pass
return mve
movie_list = {"name":"Marvel","movies": {"spiderman":"Tom","Thor":"Chris"},"Review":"5star"}
movie_data(movie_list)
Output: Output:
{'name': 'Marvel', 'movies': {'spiderman': 'Tom', 'Thor': 'Chris'}, 'Review': '5star'}
Expected Output:预期 Output:
{"name":"Marvel","movies": {"spiderman":"Tom","Thor":"Chris"},"Review":"5star"}
I'm not sure what your motivation is, but the only time I want this is when I want my dictionary to be copy/pastable for JSON files.我不确定你的动机是什么,但我唯一想要的是当我希望我的字典可以复制/粘贴 JSON 文件时。 It turns out the json
module happens to do both in your case:事实证明json
模块恰好在您的情况下同时执行这两种操作:
import json
def movie_data(movie_list):
mve = {}
for k, v in movie_list.items():
if type(v) == str:
mve[k] = str(v) if v else None
elif type(v) == dict:
mve[k] = movie_data(v)
else:
pass
return mve
movie_list = {
"name": "Marvel",
"movies": {"spiderman": "Tom", "Thor": "Chris"},
"Review": "5star",
}
print(json.dumps(movie_data(movie_list)))
# {"name": "Marvel", "movies": {"spiderman": "Tom", "Thor": "Chris"}, "Review": "5star"}
Try using json
package.尝试使用json
package。
import json
a = movie_data(movie_list)
print(json.dumps(a))
Python uses single quotes or double quotes for strings: "abc" == 'abc'
. Python 对字符串使用单引号或双引号: "abc" == 'abc'
。
By default, print
will display single quotes, unless the string already contains quotes:默认情况下, print
将显示单引号,除非字符串已经包含引号:
>>> "123"
'123'
>>> d = {'a': "b"}
>>> print(d)
{'a': 'b'}
>>> d
{'a': 'b'}
So those outputs are equal.所以这些输出是相等的。 The difference comes from Python's print function.区别来自 Python 的 print function。
>>> a = {'name': 'Marvel', 'movies': {'spiderman': 'Tom', 'Thor': 'Chris'}, 'Review': '5star'}
>>> b = {"name":"Marvel","movies": {"spiderman":"Tom","Thor":"Chris"},"Review":"5star"}
>>> a == b
True
If you print a dictionary, it will use the dictionary method __str__
to convert the dictionary to a printable string.如果打印字典,它将使用字典方法__str__
将字典转换为可打印字符串。 The __str__
method is the one using single quotes. __str__
方法是使用单引号的方法。
movie_list = {"name":"Marvel","movies": {"spiderman":"Tom","Thor":"Chris"},"Review":"5star"}
print(movie_list.__str__())
What you can do to get double quotes is to:获得双引号的方法是:
Here's the code to do it:这是执行此操作的代码:
movie_list = {"name":"Marvel","movies": {"spiderman":"Tom","Thor":"Chris"},"Review":"5star"}
movie_list_str = movie_list.__str__().replace("'", '"')
print(movie_list_str)
And the output is: output 是:
{"name": "Marvel", "movies": {"spiderman": "Tom", "Thor": "Chris"}, "Review": "5star"}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.