Python replace single backslash

Python: Replacing backslashes to avoid escape sequences in string [duplicate]

I´m trying to replace the single backslashes i get within a string with double backslashes, because sometimes the the «backslash+character» combination creates an escape sequence. I have tried various ways (mostly from other stackoverflow questions), but nothing gets me the correct results so far. Example s = «\aa, \bb, \cc, \dd»

replaces the first a and b with special characters (can´t get pasting the exact result here to work?):

print s.encode("string_escape") 
escape_dict= def raw(text): """Returns a raw string representation of text""" new_string='' for char in text: try: new_string+=escape_dict[char] except KeyError: new_string+=char return new_string 
import re import codecs ESCAPE_SEQUENCE_RE = re.compile(r''' ( \\U. # 8-digit hex escapes | \\u. # 4-digit hex escapes | \\x.. # 2-digit hex escapes | \\7 # Octal escapes | \\N\<[^>]+\> # Unicode characters by name | \\[\\'"abfnrtv] # Single-character escapes )''', re.UNICODE | re.VERBOSE) def decode_escapes(s): def decode_match(match): return codecs.decode(match.group(0), 'unicode-escape') return ESCAPE_SEQUENCE_RE.sub(decode_match, s) 

If you have this problem of problems with escape sequences in your input data, you should actually get that data fixed at the source.

thanks, i´m no sure (how) i can change the input, as it´s the string-output from a tool parameter (value table in arcgis) and i need the string representation. my question is more or less, why won´t any of these methods above work on a general example?

The question does not make sense as asked. If you «are getting» strings from an outside source, then it either actually contains a backslash followed by whatever, or else it actually contains a special character. It cannot contain an «escape sequence»; those are only relevant to string literals in your code. If your input is supposed to contain e.g. backslash followed by lowercase a, but actually contains a BEL character, then you will have to replace it manually, following your own heuristics.

There is no way to know in general what the «unescaped» version should be, because there are multiple possible string literals for any given string (except the empty string, assuming you don’t care which quotes are used). a = ‘\x20’ and a = ‘ ‘ cause a to have the same value. Suppose you know that someone else’s broken code will take in an input of backslash, lowercase x, two, zero, and inappropriately convert it to a space; suppose you receive a space from that process. It is not possible to tell whether that conversion happened or not. Information was lost.

«replaces the first a and b with special characters» No, it does not. They already were special characters.

Источник

python replace single backslash with double backslash [duplicate]

In python, I am trying to replace a single backslash («\») with a double backslash(«\»). I have the following code:

directory = string.replace("C:\Users\Josh\Desktop\20130216", "\", "\\") 

I don’t understand the point of this question. Also why does a basic misunderstanding of escape characters deserve a 50 point bounty?

just trying to get more/better suggestions, as these don’t solve my problem. Figured it’s worth a try, to avoid asking a brand new question.

9 Answers 9

No need to use str.replace or string.replace here, just convert that string to a raw string:

>>> strs = r"C:\Users\Josh\Desktop\20130216" ^ | notice the 'r' 

Below is the repr version of the above string, that’s why you’re seeing \\ here. But, in fact the actual string contains just ‘\’ not \\ .

>>> strs 'C:\\Users\\Josh\\Desktop\\20130216' >>> s = r"f\o" >>> s #repr representation 'f\\o' >>> len(s) #length is 3, as there's only one `'\'` 3 

But when you’re going to print this string you’ll not get ‘\\’ in the output.

>>> print strs C:\Users\Josh\Desktop\20130216 

If you want the string to show ‘\\’ during print then use str.replace :

>>> new_strs = strs.replace('\\','\\\\') >>> print new_strs C:\\Users\\Josh\\Desktop\\20130216 

repr version will now show \\\\ :

>>> new_strs 'C:\\\\Users\\\\Josh\\\\Desktop\\\\20130216' 

How do you make sure that an existing ‘\\’ doesn’t get doubled? Eg. you want only a maximum of double-backslash (in print version)?

I have a script where I need the stored filepath to contain double-\\’s (to send to a program that requires this escaping), but the input file path sometimes already contains this (provided by user/Python script), and sometimes doesn’t (contains only single-\’s). I want to sanitize the input to make sure all \’s are converted to exactly \\. Eg. The user might provide \\’s if they are aware of the program’s \\ syntax, but if they don’t they will provide single \’s. I could require only single-\’s all the time, but thought it mightn’t be too hard to sanitize this properly for either case.

@Demis: Have a look at os.path.normpath(path). This removes all duplicate backslashes. Afterwards, replace the single backslash with the double backslash.

Thanks Demis and @Cerno. The other answers using ‘r’ to prefix a string or the re regex library answer the initial question perfectly. But I was looking for how to handle this with user input(). Using os.path.normalpath() was exactly what I needed.

Let me make it simple and clear. Lets use the re module in python to escape the special characters.

import re s = "C:\Users\Josh\Desktop" print s print re.escape(s) 
C:\Users\Josh\Desktop C:\\Users\\Josh\\Desktop 

Now observe that re.escape function on escaping the special chars in the given string we able to add an other backslash before each backslash, and finally the output results in a double backslash, the desired output.

Please provide some context and explanation to go with your code. Also check the formatting of your code.

That’s great that there’s a function to do this for you! This is exactly what I needed, and is much prettier than the quadruple backslashes. Simple and clear indeed.

re.escape also escapes . so print(re.escape(‘bob.txt’) outputs ‘bob\.txt’ Escape is intended to escape arbitrary strings to be used in regular expressions. It is not intended to escape file paths.

Use escape characters: «full\\path\\here» , «\\» and «\\\\»

In python \ (backslash) is used as an escape character. What this means that in places where you wish to insert a special character (such as newline), you would use the backslash and another character ( \n for newline)

With your example string you would notice that when you put «C:\Users\Josh\Desktop\20130216» in the repl you will get «C:\\Users\\Josh\\Desktop\x8130216» . This is because \2 has a special meaning in a python string. If you wish to specify \ then you need to put two \\ in your string.

The other option is to notify python that your entire string must NOT use \ as an escape character by pre-pending the string with r

This is a «raw» string, and very useful in situations where you need to use lots of backslashes such as with regular expression strings.

In case you still wish to replace that single \ with \\ you would then use:

Notice that I am not using r’ in the last two strings above. This is because, when you use the r’ form of strings you cannot end that string with a single \

Источник

Want to replace backslash in python3 string

From this string I want to replace any occurrence of backslash when it appears before » with a pipe |. I wrote the following code but it actually takes out » and leaves the \ behind.

import re str = "\"abc INC\\\",\"None\", \"0\", \"test\"" str = re.sub("(\\\")", "|", str) print(str) Output: |abc INC\|,|None|, |0|, |test| Desired Output: "abc INC|","None", "0", "test" 

please use ‘ to delimite your python string if there are » inside, it will be clearer to see what are your strings

4 Answers 4

See Jamie Zawinksi’s famous quote about regular expressions. Try to only resort to the use of re’s when absolutely necessary. In this case, it isn’t.

The actual content of string str (bad name for a variable, by the way, since there’s a built-in type of that name) is

which will do exactly what you want.

Thank you. I didn’t think about it. And I will keep in mind «Try to only resort to the use of re’s when absolutely necessary.»

You can use the following positive lookahead assertion ‘\\(? \»abc INC\\\»,\»None\», \»0\», \»test\»» p = re.sub(r’\\(?=»)’, ‘|’, my_str) print(p) # ‘»abc INC|»,»None», «0», «test»‘

Try not to use builtin names as names for variables, viz. str , to avoid shadowing the builtin.

This must solve your problem:

import re s = "\"abc INC\\\",\"None\", \"0\", \"test\"" s = re.sub(r"\\", "|", s) 

Also don’t use str as a variable name, it is a reserved keyword.

For literal backslashes in python regexes you need to escape twice, giving you the pattern ‘\\\\»‘ or «\\\\\»» . The first escaping is needed for python to actually put a backslash into the string. But regex patterns themself use backshlashes as a special character (for things like \w word characters, etc.). The documentation states:

The special sequences consist of ‘\’ and a character from the list below. If the ordinary character is not on the list, then the resulting RE will match the second character.

So the pattern \» will match a single » because » is not a character with a special meaning there.

You can use the raw notation to only escape once: r’\\»‘ .

Источник

Читайте также:  Вытащить часть строки python
Оцените статью