Python3:
>>> b'\\u201c'.decode('unicode_escape')
'โ'
or
>>> '\\u201c'.encode().decode('unicode_escape')
'โ'
Answer from max5555 on Stack OverflowPython3:
>>> b'\\u201c'.decode('unicode_escape')
'โ'
or
>>> '\\u201c'.encode().decode('unicode_escape')
'โ'
You can try codecs.escape_decode, this should decode the escape sequences.
string - python replace single backslash with double backslash - Stack Overflow
Replacing double slash with single backslash
How to use replace double backslashes to single one for byte string in Python - Stack Overflow
python - How to replace double backslash to single backslash - Stack Overflow
why not use string.replace()?
>>> s = 'some \\\\ doubles'
>>> print s
some \\ doubles
>>> print s.replace('\\\\', '\\')
some \ doubles
Or with "raw" strings:
>>> s = r'some \\ doubles'
>>> print s
some \\ doubles
>>> print s.replace('\\\\', '\\')
some \ doubles
Since the escape character is complicated, you still need to escape it so it does not escape the '
Just use .replace() twice!
I had the following path: C:\\Users\\XXXXX\\Desktop\\PMI APP GIT\\pmi-app\\niton x5 test data
To convert \ to single backslashes, i just did the following:
path_to_file = path_to_file.replace('\\','*')
path_to_file = path_to_file.replace('**', '\\')
first operation creates ** for every \ and second operation escapes the first slash, replacing ** with a single \.
Result:
C:**Users**z0044wmy**Desktop**PMI APP GIT**pmi-app**GENERATED_REPORTS
C:\Users\z0044wmy\Desktop\PMI APP GIT\pmi-app\GENERATED_REPORTS
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'
Let me make it simple and clear. Lets use the re module in python to escape the special characters.
Python script :
import re
s = "C:\Users\Josh\Desktop"
print s
print re.escape(s)
Output :
C:\Users\Josh\Desktop
C:\\Users\\Josh\\Desktop
Explanation :
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.
Hope this helps you.
\\ is not double backslash but one escaped. Look:
print b'Z\xa6\x97\x86j2\x08q\\r\xca\xe6m'
# Z๏ฟฝ๏ฟฝ๏ฟฝjq\r๏ฟฝ๏ฟฝm
And \r (from your desired output) is not 2 chars but one:
print b'Z\xa6\x97\x86j2\x08q\r\xca\xe6m'
# ๏ฟฝ๏ฟฝm๏ฟฝjq
(When printing it to terminal, carriage return \r prevents us from seen the first letter Z)
If you really want to replace '\\r' with '\r', you can do:
print repr(word.replace('\\r', '\r'))
# 'Z\xa6\x97\x86j2\x08q\r\xca\xe6m'
print word.replace('\\r', '\r')
# ๏ฟฝ๏ฟฝm๏ฟฝjq
Or, if you want to replace all the escape sequences. Python2 version:
print repr(b'1\\t2\\n3'.decode('string_escape'))
# '1\t2\n3'
print b'1\\t2\\n3'.decode('string_escape')
# 1 2
# 3
Python3 version:
print(repr(b'1\\t2\\n3'.decode('unicode_escape')))
# '1\t2\n3'
print(b'1\\t2\\n3'.decode('unicode_escape'))
# 1 2
# 3
your \r is a carriage return character. So \\r is \ plus carriage return. You won't find \\ in your string.
What "works" is to replace backslash+CR by just CR:
word = b'Z\xa6\x97\x86j2\x08q\\r\xca\xe6m'
print(word.replace(b"\\r",b"\r"))
result:
b'Z\xa6\x97\x86j2\x08q\r\xca\xe6m'
but I'm not sure that's what you meant from the start (that is: inserting a carriage return char in your bytes string)
Take a closer look at the string, they are all single slash.
In [26]: my_str[0]
Out[26]: '\\'
In [27]: my_str[1]
Out[27]: 'x'
In [28]: len(my_str[0])
Out[28]: 1
And my_str.replace('\\','\') won't work because the token here is \', which escapes ' and waits for the another closing '.
Use my_str.replace('\\', '') instead
Update: after few more days, I realize the following discussion may also be helpful. If the intension of a string with escape ('\\x' or '\\u') are eventually hex/unicode literals, they can be decoded by escape_decode.
import codecs
print(len(b'\x32'), b'\x32') # 1 hex literal, '\x32' == '2'
print(len(b'\\x32'), b'\\x32') # 4 chars including escapes
print(codecs.escape_decode('\\x32', 'hex')) # chars->literal, 4->1
# 1 b'2'
# 4 b'\\x32'
# (b'2', 4)
s = '\\xa5\\xc0\\xe6aK\\xf9\\x80\\xb1\\xc8*\x01\x12$\\xfbp\x1e(4\\xd6{;Z'
ed, _ = codecs.escape_decode(s, 'hex')
print(len(s), s)
print(len(ed), ed)
# 49 \xa5\xc0\xe6aK\xf9\x80\xb1\xc8*$\xfbp(4\xd6{;Z
# 22 b'\xa5\xc0\xe6aK\xf9\x80\xb1\xc8*\x01\x12$\xfbp\x1e(4\xd6{;Z'
If you do
s = '\\xa5\\xc0\\xe6aK\\xf9\\x80\\xb1\\xc8*\x01\x12$\\xfbp\x1e(4\\xd6{;Z\\x'
s = s.replace('\\','\')
print(s)
you get
File "main.py", line 3
s = s.replace('\\','\')
^
SyntaxError: EOL while scanning string literal
because in '\' the \ escapes the ' . Your string is left open.
You do not have any double \ in s - its just displaying it as such, do distinguish it from \ used to escape stuff if you inspect it.
If you print(s) you get \xa5\xc0\xe6aK\xf9\x80\xb1\xc8*$\xfbp(4\xd6{;Z\x
The goal: 'apple' -> 'app\e'
This doesn't work:
>>> "apple".replace('l', '\\')
'app\\e'This also doesn't work:
>>> "apple".replace('l', '\')
File "<stdin>", line 1
"apple".replace('l', '\')
^
SyntaxError: EOL while scanning string literalI'm concatenating a filepath to open an image with plt.imshow() as follows:
from pathlib import Path
import numpy as np
import pandas as pd
from scipy.io import loadmat
import pprint
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import os
folder = Path('D:\ ... \emotic')
#Note that \ ... \ stands for my filepath
original_database = Path(annotations_dict['train']['original_database'][0][50][0][0][0][0])
filename = Path(annotations_dict['train']['filename'][0][50][0])
image_path = os.path.join(folder, original_database, 'images', filename)
img = mpimg.imread(image_path)
imgplot = plt.imshow(img)
plt.show()
print(image_path)
print(repr(image_path))But I consistently get the following error message: UnidentifiedImageError*: cannot identify image file 'D:\\ ... \\ ... \\ ... \\ ... \\ ... \\emotic\\framesdb\\images\\frame_aztc78d4kjclxv9e.jpg'*
So obviously it can't read the filepath because all the backslashes are doubled. I've found many posts online explaining why this happens (\ is an escape character), but none explaining how to solve this. How do I enforce that the filepath passed on to the plt.imshow() function contains only single backslashes? I've tried solving this issue using the following methods:
-
os.path.normpath( ... ) ==> does nothing;
-
image_path.replace('\\', '\') ==> also does nothing;
-
using r'...' to enforce a raw string with single backslashes ==> still does nothing.
Note also that print(image_path) gives the filepath with single backslashes, while print(repr(image_path)) gives the filepath with double backslashes. And I kinda get the difference and why this is (kinda). But I don't know how to pass on the one with the single backslashes to plt.imshow(), and every method I try ends up giving me the same "double slash" error.
Any help would be greatly appreciated :D
EDIT 1 : The filepath named in the error message is correct. If I take that filepath and replace all the double slashes by single slashes, then put this into my file explorer, the correct image comes up.
EDIT 2 [SOLVED] : Turns out the error wasn't due to single vs. double backslashes in the filepath, but rather to a problem with the matplotlib.image.imread() function that for some reason can't access my image database and triggers the error. If I use an image reader from the PIL library instead, everything works fine:
Solved using this:
from PIL import Image plt.imshow(Image.open(image_path))
instead of this:
plt.imshow(mpimg.imread(image_path))
Thanks to everyone for helping me isolate the issue!
Not sure if this is appropriate for your situation, but you could try using unicode_escape:
>>> t
'\\u0048\\u0065\\u006c\\u006c\\u006f\\u0020\\u20ac\\u0020\\u00b0'
>>> type(t)
<class 'str'>
>>> enc_t = t.encode('utf_8')
>>> enc_t
b'\\u0048\\u0065\\u006c\\u006c\\u006f\\u0020\\u20ac\\u0020\\u00b0'
>>> type(enc_t)
<class 'bytes'>
>>> dec_t = enc_t.decode('unicode_escape')
>>> type(dec_t)
<class 'str'>
>>> dec_t
'Hello โฌ ยฐ'
Or in abbreviated form:
>>> t.encode('utf_8').decode('unicode_escape')
'Hello โฌ ยฐ'
You take your string and encode it using UTF-8, and then decode it using unicode_escape.
Since a backslash is an escape character and you are searching for two backslashes you need to replace four backslashes with two - i.e.:
t.replace("\\\\", "\\")
This will replace every r"\\" with r"\". The r indicates raw string. So, for example, if you type print(r"\\") into idle or any python script (or print r"\\" in Python 2) you will get \\\\. This means that every "\\" is really just a r"\".
user1632861 suggested that you use .replace("\\", ""), but this replaces ever r"\" with nothing. Try the above method instead. :D
In this case, however, it appears as though you are reading/receiving data, and you probably want to use the correct encoding and then decode to unicode (as the person above me suggested).