Python Forum
python display with '\\' when prints with key-value in dictionary
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python display with '\\' when prints with key-value in dictionary
#1
#!/bin/python

dict = {
   'name': ""
}

dict['name'] = "{}".format("\python")
print(dict)
here is the output:
{'name': '\\python'}
The above code snippet, here have created a dictionary of one key and assigned a value to it. When I print the value of dict['name'] it prints '\python', however when it prints with dict and it prints with '\\python'. And this is python 2.

Can any one please help me out to solve this issue. Thanks in advance.
Regards,
Maiya
Reply
#2
This is not an issue. If you ask for dict['name'] it returns '\python'. If you ask for dict.values() it will return '\python'. Only when you print the dictionary, something you almost only never do except for debugging, will you ever see '\\python'.
Reply
#3
Here it is just one key is present in dictionary. Where in the dictionary having the 50/100 keys and sub dictionaries, in that scenario I cannot print each key and its values, instead will print single dictionary at one go as a dict only.

There will face that issue, so how to avoid printing this '\\' when I print whole dictionary itself.
Reply
#4
The backslash character has special meaning (escape character), and python is inserting another backslash to say "The next backslash is just a backslash". I do not know why this is necessary, but it must be for some reason. Interestingly python only does this when the next letter is not part of a escape sequence. For example, if I use a value that starts with \n or \r, these do not get the extra backslash. My guess is python is doing you a favor and automatically adding the extra backslash to prevent anyone from treating it as an invalid escape sequence. You should be adding the extra backslash yourself.

It is easy to print out an entire dictionary. And you can use much nicer formatting if you want.

for key, value in dict.items()
    print(key, value)
Just thought of something you could do. Use a raw string. A backslash in a raw string is just a backslash. That must be what Python is doing, saving dictionary keys as raw strings.
Reply
#5
This is again printing each keys and value through loop, not in a single go.

but the wonder is why dictionary is adding extra slash to that when it is printing whole dictionary. But when print that key alone it won't add that extra slash.

I know the meaning of escape sequence n all, am a Core C and C++ developer. but just a wonder to know that why python is adding to that and how to avoid printing extra slash.

and mean time I wanted to print dictionary at a single go, not in any loop.

if any one knows, how to print the whole dictionary at one go and without adding extra slash as well.

like for the same below program I want the below output format
#!/bin/python

dict = {
'name': ""
}

dict['name'] = "{}".format("\python")
print(dict)

output: {'name': '\python'}

but not as an -- {'name': '\\python'}.

if any one knows, how to avoid to print extra slash, plz do let me know. Thanks in advance.
Reply
#6
Has nothing to do with dictionaries. This does the same thing:
value = '\python'
value
'\\python'

I found this:

"The extra backslash is not actually added; it's just added by the repr() function to indicate that it's a literal backslash. The Python interpreter uses the repr() function (which calls __repr__() on the object) when the result of an expression needs to be printed:"

This makes sense. When you use print(str), python uses str.__str__(). I guess when you use print(dict) python uses __repr__ for the keys and values thinking that you are interested in the details.

I think it is time to write a nice short function that will print your directories. Maybe take a look at pprint or json.dumps.
Reply
#7
Cool, response. what you said it is absolutely. However it is same with pprint as well. So let me see some thing different which I can do it my own to print the dictionaries.

The problem, printing can do it any ways, but at the same I also wanted to return the dict to other modules (applications), so how could I do that. Since when return this dict, again the same problem in the application level. So that is where my concern.
Reply
#8
The value in the dictionary is '\python'. Anything that uses the value will get '\python'. It is only the annoying print(dict) that calls __repr__ instead of __str__ where we see '\\python', and that is just an artifact of using __repr__ instead of __str__ to get a string to print. I would not worry about passing the dictionary to other modules. You aren't printing the dictionary to get access to the keys and values, are you?
Reply
#9
Yes, we are passing this dictionary to another application, and there in that retrieving/accessing the keys, values and printing it. And while printing it display with '\\' instead of '\' as well.
Reply
#10
Don't see the problem here as mention bye @deanhystad so do dictionary or any data collection show the repr() values.
That's because dictionaries are not end-user representable data structures,more like debug friendly for developers.
Usually for end-user there is print() of data and then is show \python.

If want to show dictionary for some reason that i don't see why,can make rebuild output it with print().
Don't use dict as a variable name as it's a reserved word in Python.
>>> my_dict = {'name': '\python'}
>>> my_dict
{'name': '\\python'}

>>> print(''.join(f'{{"{k}": "{v}"}}' for k,v in my_dict.items()))
{"name": "\python"}
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to display <IPython.core.display.HTML object>? pythopen 3 45,696 May-06-2023, 08:14 AM
Last Post: pramod08728
  zfill prints extra et the end of a var tester_V 4 848 Mar-24-2023, 06:59 PM
Last Post: tester_V
  How can I display dictionary in a gui window in Python? C0D3R 2 1,661 Apr-07-2022, 07:33 PM
Last Post: C0D3R
  variable prints without being declared. ClockPillow 2 1,770 Jul-11-2021, 12:13 AM
Last Post: ClockPillow
  python prints none in function output chairmanme0wme0w 3 2,157 Jul-07-2021, 05:18 PM
Last Post: deanhystad
  Output prints Account.id at the end? LastStopDEVS 5 2,716 Dec-19-2020, 05:59 AM
Last Post: buran
Information Unable to display joystick's value from Python onto display box MelfoyGray 2 2,172 Nov-11-2020, 02:23 AM
Last Post: MelfoyGray
  Try/Exept prints only ones tester_V 11 3,737 Nov-03-2020, 02:38 AM
Last Post: tester_V
  loop only prints last character. mcmxl22 1 1,674 Feb-17-2020, 02:36 AM
Last Post: menator01
  How To Display this Python Code in Web browser? pradhan 1 3,272 Jul-14-2019, 06:59 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020