简体   繁体   English

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

[英]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:获得双引号的方法是:

  1. get the string that will get printed获取将要打印的字符串
  2. 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)

And the output is: output 是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用正则表达式将 Python 中的转义双引号替换为单引号 - Replace escaped double quotes to single quotes in Python using regex Python列表:用单引号代替双引号 - Python List: Replace Double Quotes with Single Quotes 如何在 python 数组中用双引号替换单引号而不替换 json 值中的单引号? - How do I replace single quotes with double quotes in a python array without replacing the single quotes in the json values? 如何将dataframe中的2个双引号替换成单双引号? - How to replace 2 double quotes into single double quotes in dataframe? 在python中将单引号替换为双引号,以便与插入数据库一起使用 - Replace single quotes with double quotes in python, for use with insert into database 如何将python中的单引号(')替换为\'? - How to replace single quotes (') with \' in python? 如何使用 python 替换列表中的特殊字符(双引号到单引号),以便我的列表成为二维列表? - How to replace special characters(double quotes to single quotes) from a list using python so that my list become a 2D list? 在python中用'\\''替换双引号 - replace double quotes with '\"' in python 如何用带引号的双引号替换字符串中的单引号 - How to replace single quotes within a string with quoted double quotes 如何将 dataframe 中的 2 个双引号替换为单引号? - how to replace 2 double quotes into single in dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM