相关文章推荐
温文尔雅的橡皮擦  ·  Flourish·  9 月前    · 
近视的春卷  ·  孙宝国_百度百科·  9 月前    · 
低调的打火机  ·  C++ ...·  1 年前    · 

使用python,如何将单引号替换为双引号

2 人关注

我是用双引号输入的,在处理一些操作后,需要用双引号显示输出,而它是用单引号格式的。

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:
    return mve
movie_list = {"name":"Marvel","movies": {"spiderman":"Tom","Thor":"Chris"},"Review":"5star"}
movie_data(movie_list)

Output:

{'name': 'Marvel', 'movies': {'spiderman': 'Tom', 'Thor': 'Chris'}, 'Review': '5star'}

Expected Output:

{"name":"Marvel","movies": {"spiderman":"Tom","Thor":"Chris"},"Review":"5star"}
    
4 个评论
引号不在字典里。这只是 Python 打印字符串的方式。
单引号和双引号在 Python 中是等价的,为什么当你打印字典时,它使用哪个有关系呢?
如果你想创建JSON,请使用 json 模块。
python
python-3.x
dictionary
user10389226
user10389226
发布于 2022-01-08
4 个回答
user1717828
user1717828
发布于 2022-01-08
0 人赞同

我不知道你的动机是什么,但我唯一想这样做的时候是当我想让我的字典对JSON文件进行复制/粘贴。 事实证明,在你的情况下, 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:                                                                   
    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"}
    
span
span
发布于 2022-01-08
0 人赞同

尝试使用 json 包。

import json
a = movie_data(movie_list)
print(json.dumps(a))
    
groumache
groumache
发布于 2022-01-08
0 人赞同

如果你打印一个字典,它将使用字典方法 __str__ 将字典转换为可打印的字符串。替换代码0】的方法是使用单引号的方法。

movie_list = {"name":"Marvel","movies": {"spiderman":"Tom","Thor":"Chris"},"Review":"5star"}
print(movie_list.__str__())

要获得双引号,你可以做的是:。

  • get the string that will get printed
  • replace the double quotes by single quotes
  • 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)
    

    而输出是。

    {"name": "Marvel", "movies": {"spiderman": "Tom", "Thor": "Chris"}, "Review": "5star"}
        
    这是超级整洁的。 谢谢你的贡献!
    qkzk
    qkzk
    发布于 2022-01-08
    0 人赞同

    Python uses single quotes or double quotes for strings : "abc" == 'abc' .

    默认情况下, print 将显示单引号,除非该字符串已经包含引号。

    >>> "123"
    '123'
    >>> d = {'a': "b"}
    >>> print(d)
    {'a': 'b'}
    {'a': 'b'}
    

    所以这些输出是相等的。差异来自于Python的打印函数。