0

I ve been trying to delete all the "\" from my string in python but it seems it is not working.The object is initially a bytes type and I converted it to string before trying the following methods :

str(self.solution).string("\\")

str(self.solution).replace("\\","")

am i doing something wrong here ? both methods didn´t work. This is the code I am using :

cmd = ( "cd ~/rubiks-cube-NxNxN-solver/; ./usr/bin/rubiks-cube-solver.py --colormap '%s' --state %s"
                            % (json.dumps(colormap), kociemba_string)
                        )

                        log.info(cmd)
                        output = check_output(cmd, shell=True)

                        for line in output.splitlines():
                            self.solution = line.strip()

                        if self.size >= 4:
                            self.solution = (
                                "See /tmp/rubiks-cube-NxNxN-solver/index.html"
                            )
                    print(self.solution)
            sendSolutionToArduino(self.solution[10:])
Tom Karzes
  • 22,815
  • 2
  • 22
  • 41
Zorghost
  • 29
  • 7
  • As always, in a non-raw string you need to double up backslashes. – Charles Duffy Feb 01 '23 at 01:36
  • You want `self.solution.decode().replace("\\", "")`; you don't use plain `str` to convert `bytes` to `str`, and you have to escape the backslash in the literal for the replacement. Raw string literals aren't an option here, since they can't *end* in a backslash. – ShadowRanger Feb 01 '23 at 01:36
  • 2
    By the way, it's a serious smell for security problems to be substituting values into a shell command like this. – Charles Duffy Feb 01 '23 at 01:36
  • I tried your solution @ShadowRanger but didn´t work – Zorghost Feb 01 '23 at 01:38
  • The problems you have here aren't about backslashes that come before quotes. – Charles Duffy Feb 01 '23 at 01:40
  • can you tell me what s the problem please ? @CharlesDuffy – Zorghost Feb 01 '23 at 01:41
  • been 5 hours troubleshooting here – Zorghost Feb 01 '23 at 01:41
  • try using a raw string like `r"\"` – Caridorc Feb 01 '23 at 01:42
  • You just casted a `bytes` to `str` incorrectly, you probably want to follow [this thread](https://stackoverflow.com/questions/606191/convert-bytes-to-a-string). – metatoaster Feb 01 '23 at 01:42
  • 2
    @Caridorc: Raw string literals can't *end* with a backslash, as I've already mentioned. It's a flaw in the grammar; backslashes in raw strings only escape the quote character, but there's no way to *prevent* that escaping. – ShadowRanger Feb 01 '23 at 01:45
  • 1
    (Mind, I'm making a lot of guesses here because the code in the question isn't a [mre]; in its current state we can't run it ourselves without changes to see the problem with our own eyes, and then rerun it with our changes to test a proposed fix -- thus, anyone's proposed answer will necessarily be untested) – Charles Duffy Feb 01 '23 at 01:45
  • Maybe using three backslashes? – Caridorc Feb 01 '23 at 01:46
  • `print("\\")` works for me printing a single backslash – Caridorc Feb 01 '23 at 01:51
  • @Caridorc three backslashes will mess up with the string and code can´t run – Zorghost Feb 01 '23 at 01:54
  • @metatoaster I followed the top answer and didn´t work , I can replace any character in string except the backslashes there. here is what I get : Solution : F\' R2 F2 L D2 F\' B D\' B2 R\' F\' U\' R2 D L2 D R2 U\' L2 F2 – Zorghost Feb 01 '23 at 01:57
  • `str(self.solution).string("\\")` Strings don't have a `.string()` attribute... – John Gordon Feb 01 '23 at 02:02
  • @Zorghost that's to get it to a correct form that you can apply `str.replace()`, what you had originally was probably doubly escaped `\'`. Now you might be able to call `.replace("\\", "")`. – metatoaster Feb 01 '23 at 02:08

1 Answers1

1

Keep in mind that .replace does not change the input but returns a new string with the replacement made:

x = "dfghj\\gjhk\\hhh"
print("before", x)
x = x.replace("\\", "")
print("after", x)

Output:

before dfghj\gjhk\hhh
after dfghjgjhkhhh

But:

x = "dfghj\\gjhk\\hhh"
print("before", x)
x.replace("\\", "") # this value is computed and immediately discarded
print("after", x)

Output:

before dfghj\gjhk\hhh
after dfghj\gjhk\hhh

If you want to replace the sequence \" with nothing then you can use:

x = "dfghj\\\"gjhk\\hhh"
print("before", x)
x = x.replace("\\\"", "")
print("after", x)

Output:

before dfghj\"gjhk\hhh
after dfghjgjhk\hhh

If you want to replace the sequence \' with nothing then you can use:

x = "dfghj\\\'gjhk\\hhh"
print("before", x)
x = x.replace("\\\'", "")
print("after", x)

Output:

before dfghj\'gjhk\hhh
after dfghjgjhk\hhh
Caridorc
  • 6,222
  • 2
  • 31
  • 46
  • Of course I know that . I double checked and tested again following the response but it didn´t work. But thank you anyways ! – Zorghost Feb 01 '23 at 02:02
  • @Zorghost do you have backslash followed by single quote? in that case `x = x.replace("\\\'", "")` might work – Caridorc Feb 01 '23 at 02:03
  • It is unfortuantely the same thing , I tested it – Zorghost Feb 01 '23 at 02:08
  • @Zorghost very weird, maybe it is a backtick? `\`` In my environment it works, are you using python 2 or 3? – Caridorc Feb 01 '23 at 02:09
  • I am using python 3 , but the code is a little bit old and have been developed using python 2 , and I am pretty sure it s the normal ' – Zorghost Feb 01 '23 at 02:10
  • If you run my examples in your enviroment do you get the same results? – Caridorc Feb 01 '23 at 02:15
  • I got the following : before krnfgrkg\\\'fsgsrgegr\\hhhh\nafter krnfgrkgfsgsrgegr\\hhhh\n on my terminal when using the second example – Zorghost Feb 01 '23 at 02:20
  • unfortuantely no , you can notice that I got an extra backslash in the after right before hhhh , I think it is something related to special characters but I m not sure what is it – Zorghost Feb 01 '23 at 02:23
  • @Zorghost I count two backslashes before hhhh, both in the before and after strings – Caridorc Feb 01 '23 at 02:24
  • yeah I m sorry my bad , it s been 8 hours since I am troubleshooting so I m a little bit tired haha – Zorghost Feb 01 '23 at 02:25
  • @Zorghost ahahah it happens to me also, do not worry about that – Caridorc Feb 01 '23 at 02:26
  • @Zorghost upvote and accept to mark that the answer is good for future visitors if it helped you – Caridorc Feb 01 '23 at 02:29