3

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

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:

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

Expected Output:

{"name":"Marvel","movies": {"spiderman":"Tom","Thor":"Chris"},"Review":"5star"}
user10389226
  • 109
  • 3
  • 14

4 Answers4

4

If you print a dictionary, it will use the dictionary method __str__ to convert the dictionary to a printable string. The __str__ method is the one using single quotes.

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:

{"name": "Marvel", "movies": {"spiderman": "Tom", "Thor": "Chris"}, "Review": "5star"}
groumache
  • 86
  • 3
2

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. It turns out the json module happens to do both in your case:

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"}
user1717828
  • 7,122
  • 8
  • 34
  • 59
2

Try using json package.

import json
a = movie_data(movie_list)
print(json.dumps(a))
span
  • 31
  • 4
0

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

By default, print will display single quotes, unless the string already contains quotes :

>>> "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.

>>> a = {'name': 'Marvel', 'movies': {'spiderman': 'Tom', 'Thor': 'Chris'}, 'Review': '5star'}
>>> b = {"name":"Marvel","movies": {"spiderman":"Tom","Thor":"Chris"},"Review":"5star"}
>>> a == b
True
qkzk
  • 185
  • 1
  • 6