0

Python 2.7. I'm trying to replace a string with a new string. This is the code:

somestring = "C:\xampp\htdocs\email\BLAHBLAH"
thestring = somestring
thenewstring = thestring.replace("C:\\xampp\\htdocs\\email\\BLAHBLAH", "something")
print thestring
print thenewstring

What I want to see printed:

C:\xampp\htdocs\email\BLAHBLAH

something

This is the error I'm getting:

ValueError: invalid \x escape

EDIT: Let's pretend that I have no access to do anything at all with the top line. It is set in stone. I'm reading in meta data from a Word document and looking for that exact string and then trying to replace it with a new string. My code's involvement starts on line 2. It is not a duplicate of that question because I am not working with a literal string. I've read through quite a few question/answers on here and I'm understanding that "\x" is some kind of special thing that replace can't find for reasons I don't understand. I've tried several of the suggestions (like using "repr", or "decode") but I can't get any of them to work.

How can I get this to work?

  • 1
    Add extra `'\'`s in the first line: `thestring = "C:\\xampp\\htdocs\\email\\BLAHBLAH"`. (You have them in the second line, why not in the first?) It also looks like you replace the _whole_ original string with another string. Why not simply assign a new string to the new variable? `thenewstring = "something"` – DYZ May 31 '17 at 02:10
  • That works but unfortunately, in the actual code, I don't have access to change what's inside "thestring". I'm reading in meta data from a Word Document and finding that exact string (which I can't change) and then replacing it with something else. – Just a Sec User May 31 '17 at 02:14
  • 2
    So, you do _not_ actually have the assignment on the first line of your example? Then please remove it (because it is causing the error) and simply print the value of `thestring`. – DYZ May 31 '17 at 02:16
  • Well, I do have the assignment, it just looks like this instead "contents = file.read().replace(search, replace)". I just changed it to make it clearer for the question. file.read() would contain "C:\xampp\htdocs\email\BLAHBLAH". – Just a Sec User May 31 '17 at 02:18
  • You have an error in one of the functions that you call. Until you find out, in which, do not lump them together. What do you see if you `print` the result of `fd.read())` before you replace anything? – DYZ May 31 '17 at 02:21
  • Unfortunately, that's not feasible for me to get. It's part of a large application that runs behind the scenes inside a docker container. I've traced it back to what I think is the problem. Is there no solution to what I asked in the original question assuming I can't change anything between the double quotes in thestring? – Just a Sec User May 31 '17 at 02:25

1 Answers1

-1

If you have an error using

thestring = "C:\xampp\htdocs\email\BLAHBLAH"

you should use this:

thestring = r"C:\xampp\htdocs\email\BLAHBLAH"

the final code that you want:

thestring = r"C:\xampp\htdocs\email\BLAHBLAH"
thenewstring = thestring.replace("C:\\xampp\\htdocs\\email\\BLAHBLAH", "something")
print thestring
print thenewstring

the output:

C:\xampp\htdocs\email\BLAHBLAH
something

Please, note the "r" before the string. If you want more information about this: What exactly do "u" and "r" string flags do in Python, and what are raw string literals?

lvvittor
  • 55
  • 6