Python Forum
suddenly '\b' become 'x08'
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
suddenly '\b' become 'x08'
#1
Hi,
I have a code like this :

a = 'D:\\AvosLab\\digital_scale\\base\\data.db'
print(a)
Output:
'D:\\AvosLab\\digital_scale\x08ase\\data.db'
how to get rid of this?
Reply
#2
I don't think the output is consistent with this code. i.e. it is from interactive prompt and the string has single \ in front of b

Python 3.5.2 (default, Jul 17 2016, 00:00:00) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 'D:\\AvosLab\\digital_scale\\base\\data.db'
>>> print(a)
D:\AvosLab\digital_scale\base\data.db
>>> a = 'D:\\AvosLab\\digital_scale\base\\data.db'
>>> print(a)
D:\AvosLab\digital_scalase\data.db
>>> a
'D:\\AvosLab\\digital_scale\x08ase\\data.db'
Reply
#3
(Oct-05-2017, 07:40 AM)buran Wrote: I don't think the output is consistent with this code. i.e. it is from interactive prompt and the string has single \ in front of b

Python 3.5.2 (default, Jul 17 2016, 00:00:00) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> a = 'D:\\AvosLab\\digital_scale\\base\\data.db'
>>> print(a)
D:\AvosLab\digital_scale\base\data.db
>>> a = 'D:\\AvosLab\\digital_scale\base\\data.db'
>>> print(a)
D:\AvosLab\digital_scalase\data.db
>>> a
'D:\\AvosLab\\digital_scale\x08ase\\data.db'

so, what should i do?
Reply
#4
for start - show us your actual code and output.

general answer would be one of this:
1. use double backslash instead of one
2.use raw string
3.use forward slash, instead of backslash
Reply
#5
The backslash is used as an escape character, so \n for example means newline. \\ is used to indicate a literal backslash. Thus, if you want to be able to print \\ you need to include \\\\ in your strings.

(When reading text from a file, python will store \ as \\ for this reason.)

You can also store raw literal string (note the r before the string):
Output:
>>> a = r'D:\\AvosLab\\digital_scale\\base\\data.db' >>> print(a) D:\\AvosLab\\digital_scale\\base\\data.db
I am trying to help you, really, even if it doesn't always seem that way
Reply
#6
(Oct-05-2017, 03:26 PM)gruntfutuk Wrote: , if you want to be able to print \\ you need to include \\\\ in your strings
No 2 is enough,no need for 4.
(Oct-05-2017, 03:26 PM)gruntfutuk Wrote: You can also store raw literal string (note the r before the string):
When using raw string you don't double up.
>>> a = r'D:\AvosLab\digital_scale\base\data.db'
>>> # It's 2
>>> a
'D:\\AvosLab\\digital_scale\\base\\data.db'

>>> # When print show 1
>>> print(a)
D:\AvosLab\digital_scale\base\data.db
Other way is also okay.
>>> a = 'D:/AvosLab/digital_scale/base/data.db'
>>> print(a)
D:/AvosLab/digital_scale/base/data.db
Just don't use single \.
>>> s = '\b'
>>> s
'\x08'
Reply
#7
(Oct-05-2017, 08:18 AM)buran Wrote: 3.use forward slash, instead of backslash
+1
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#8
(Oct-05-2017, 07:07 PM)wavic Wrote:
(Oct-05-2017, 08:18 AM)buran Wrote: 3.use forward slash, instead of backslash
+1

it's generated, I don't know how to change it to forward slash
Reply
#9
a = 'D:\\AvosLab\\digital_scale\\base\\data.db'

a = a.replace('\\', '/')

a
'D:/AvosLab/digital_scale/base/data.db'
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#10
(Nov-07-2017, 10:33 AM)wavic Wrote:
a = 'D:\\AvosLab\\digital_scale\\base\\data.db'

a = a.replace('\\', '/')

a
'D:/AvosLab/digital_scale/base/data.db'

can I go to the directory using this format ('D:/AvosLab/digital_scale/base/data.db') with python?
Reply


Forum Jump:

User Panel Messages

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