Python Forum

Full Version: suddenly '\b' become 'x08'
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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?
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'
(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?
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
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
(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'
(Oct-05-2017, 08:18 AM)buran Wrote: [ -> ]3.use forward slash, instead of backslash
+1
(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
a = 'D:\\AvosLab\\digital_scale\\base\\data.db'

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

a
'D:/AvosLab/digital_scale/base/data.db'
(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?
Pages: 1 2