Python Forum
can not change double to single dash
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
can not change double to single dash
#1
python 2.7.11

def leftrotate(l, n):
    return l[n:] + l[:n]

def rightrotate(l, n):
    return l[-n:] + l[:-n]

def encode(s, k, kk):
    l = [ord(i) for i in s]
    return leftrotate(''.join([chr(i + k) for i in l]), kk)

def decode(s, k, kk):
    l = [ord(i) for i in rightrotate(s.replace("\\\\","\\"), kk)]
    return ''.join([chr(i - k) for i in l])

>>> "\\".replace("\\\\","\\")
'\\'
>>> encode("xxxxxxQ\|2",3,7)
'_\x7f5{{{{{{T'
>>>
>>> decode("_\x7f5{{{{{{T",3,7)
'xxxxxxQ\\|2'
decode error,

then i try to patch with

decode("_\x7f5{{{{{{T",3,7).replace("\\\\","\\")
but can not change double to single

>>> "\\".replace("\\\\","\\")
'\\'
Reply
#2
Please put your code in the [python][/python] markers or all the indentation is lost...

Notice that your statement (the syntax highlighting does not like your profuse use of the backslash...)
>>> "\\".replace("\\\\","\\")
'\\'
is equivalent to:
>>> "A".replace("AA", "A")
'A'
And there is obvious that in a string of 1 char there will be 0 replacements of a 2 chars group.
Your expression works when the input has 2 or more backslashes:
>>> bs = '\\'
>>> s = bs * 8
>>> s
'\\\\\\\\\\\\\\\\'
>>> len(s)
8
>>> s.replace("\\\\", "\\")
'\\\\\\\\'
>>> len(_)
4
Reply
#3
how to make two dash to one dash
Reply
#4
In Python '\\' is a way to represent a single '\'. Since the backslash is an escape character to get a single '\' you have to escape it.
>>> print('\\')
\
>>> s = '\nGSG'
>>> print(s)

GSG
>>> s = '\\nGSG'
>>> print(s)
\nGSG
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
"\\".replace("\\\\","\\")

but in variable it still is \\
how to replace to become \

i have google to get the print standard output result into variable, but variable show it still is \\
Reply
#6
Yes in the variable you see it as '\\' but it is '\' when you print it. Just like the new line character. It has no graphical representation as the letters for example so we use '\n' to deal with it. And because the single backslash is an escape character we have to escape it with another one. So:

s = '\\no \\new line here'
print(s)
Output:
\no \new line here
But see this:
print(len(s))
Output:
18
If you count the characters in the string you will get 20 but as you can see the output they are 18. '\\' is a representation of '\'. For our convenience.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#7
i am not using print , i am used it as a password to do parameter in netmiko

how to replace \\ to \?
Reply
#8
Print function prints what it is in the string. If there is double \\ in a Python string it's actually \.
Count the characters you see then use len(password) to see do you have single backslash or double.
Or can use raw strings.

This will show you that you have \ when you see \\ in a Python string.
>>> from hashlib import sha1
>>> sha1(bytes(r'my\password'.encode('utf-8'))).hexdigest()
'05d0aaf554af6c33a5b558f3d78345b422198522'
>>> sha1(bytes('my\\password'.encode('utf-8'))).hexdigest()
'05d0aaf554af6c33a5b558f3d78345b422198522'
>>> 
So do you have single \ when you see \\ in the string? I think so.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Remove single and double quotes from a csv file in 3 to 4 column shantanu97 0 6,974 Mar-31-2021, 10:52 AM
Last Post: shantanu97
  Single digits seem to be greater than double digits Frosty_GM 4 3,498 Nov-20-2020, 10:13 AM
Last Post: DeaD_EyE
  create loop of subplot plotly dash without hardcode tonycat 0 3,908 Sep-23-2020, 08:40 AM
Last Post: tonycat
  How to update component props every time when a Dash callback returns? sguzunov 0 2,532 Jul-27-2020, 07:11 AM
Last Post: sguzunov
  Invalid archive error when attempting to install dash bootstrap components meaydemi 0 4,772 Jul-11-2019, 05:49 PM
Last Post: meaydemi
  how to use dataframe in dash? zhujp98 1 2,841 Jun-20-2018, 12:19 AM
Last Post: scidam
  dash problems zhujp98 0 2,240 Jun-14-2018, 03:30 PM
Last Post: zhujp98
  Change single element in 2D list changes every 1D element AceScottie 9 12,066 Nov-13-2017, 07:05 PM
Last Post: Larz60+
  Replace Single Backslash with Double Backslash in python saswatidas437 2 33,292 Mar-19-2017, 10:48 AM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

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