This question already has answers here:
Converting double slash utf-8 encoding
(6 answers)
Closed 4 years ago.
I have a str like "abc\\xe2\\x86\\x92", what I want is "abc→". (b"abc\xe2\x86\x92".encode() equals →)
The problem is, \\xe2\\x86\\x92 is in str, and \\ is just one character so there is no way to replace it into \.
I found a way to solve this:
In [107]: original.encode().decode('unicode-escape').encode('latin1').decode('utf-8')
Out[107]: 'abc→'
Related
This question already has answers here:
Best way to convert string to bytes in Python 3?
(5 answers)
Closed 8 months ago.
The community reviewed whether to reopen this question 8 months ago and left it closed:
Original close reason(s) were not resolved
I am able to define a bytes string as follows:
string = b"0x340xEF"
I want to cast an already made string. Let us call it string into a bytes string. How do I perform this? bytes(string) is not giving me an equivalent result.
You need to encode the string to be able to convert it to bytes:
string = "b0x340xEF".encode("latin-1")
print(string)
Output:
b'b0x340xEF'
This question already has answers here:
Convert a Unicode string to a string in Python (containing extra symbols)
(12 answers)
Closed 3 years ago.
I need to convert word
name = 'Łódź'
to ASCII characters
output: 'Lodz'
I can't import any library like unicodedata.
I need to do it in clear python.
I've tried to encode than decode and nothing worked.
Well, a simple method would be to map and replace. This also does not require any special imports.
name = 'Łódź'
name=name.replace('Ł','L')
name=name.replace('ó','o')
name=name.replace('ź','z')
print(name)
This question already has answers here:
Decode HTML entities in Python string?
(6 answers)
Closed 3 years ago.
I have a string "hello[ World]" and I want to convert it to "hello[World]"
I tried something like this:
a.encode("utf-8").decode("ascii")
I got back same string as input.
Try this:
import html
html.unescape("LASIX [FUROSEMIDE]")
This produces:
'LASIX [FUROSEMIDE]'
This question already has answers here:
How to convert hexadecimal string to bytes in Python?
(7 answers)
Closed 5 years ago.
I want to convert a hexadecimal string like 1030 to a byte array like b'\x10\x30'
I know we can use bytearray.fromhex("1030") or "1030".decode("hex"). However, I get output '\x100'.
What am I missing here?
bytearray(b'\x100') is correct, you just interpret it wrong way. It is character \x10 followed by character 0 (which happens to be ASCII for \x30).
There is a built-in function in bytearray that does what you intend.
bytearray.fromhex("de ad be ef 00")
It returns a bytearray and it reads hex strings with or without space separator.
This question already has answers here:
What is the difference between UTF-8 and ISO-8859-1? [closed]
(8 answers)
Converting Byte to String and Back Properly in Python3?
(2 answers)
Process escape sequences in a string in Python
(8 answers)
Closed 7 months ago.
In Python 3, I have a string like the following:
mystr = "\x00\x00\x01\x01\x80\x02\xc0\x02\x00"
This string was read from a file and it is the bytes representation of some text. To be clear, this is a unicode string, not a bytes object.
I need to transform mystr into a bytes object like the following:
mybytes = b"\x00\x00\x01\x01\x80\x02\xc0\x02\x00"
Notice that the translation should be literal. I don't want to encode the string.
Running .encode('utf-8') will escape the \.
It I manually copy and past the content into a bytes string, then everything works. What I couldn't find anywhere is how could I convert it without copy+paste.
mystr.encode("latin-1") is what you want.