My problem String
# -*- coding: utf-8 -*-
print ("################################")
foo = "СТ142Н.0000"
print (type(foo))
print("foo: "+foo)
foo_l = []
foo_l.append(foo)
print ("List: " )
print (foo_l)
print ("List decode: ")
print([x.decode("UTF-8") for x in foo_l])
print("Pop: "+foo_l.pop())
Print result:
################################
<type 'str'>
foo: СТ142Н.0000
List:
['\xd0\xa1\xd0\xa2142\xd0\x9d.0000']
List decode:
[u'\u0421\u0422142\u041d.0000']
Pop: СТ142Н.0000
This works fine , I just write the string "CT142H.0000" by hand tiping with keyboard (Its the same code)
print ("################################")
foo = "CT142H.0000"
print(type(foo))
print("foo: "+foo)
foo_l = []
foo_l.append(foo)
print ("List: ")
print (foo_l)
print ("List decode: ")
print([x.decode("UTF-8") for x in foo_l])
print("Pop: "+foo_l.pop())
Print result:
################################
<type 'str'>
foo: CT142H.0000
List:
['CT142H.0000']
List decode:
[u'CT142H.0000']
Pop: CT142H.0000
Why python encode the first string when I append it into a list?
This is currently solved, I was worried about thats chars, I put the "result" in JSON and then in a website, finally in the website it works fine!
I found another solution, but no is a correct solution, cause you will have problems in some cases.
json.dumps(list, ensure_ascii=False)
Thanks for all!
Since Python 3.0, strings are stored as Unicode, i.e. each character in the string is represented by a code point. So, each string is just a sequence of Unicode code points. For efficient storage of these strings, the sequence of code points is converted into a set of bytes. The process is known as encoding.
Introduction In Python, the string is an immutable object. You can use the '+' operator to append two strings to create a new string.
Python String encode() Method The encode() method encodes the string, using the specified encoding. If no encoding is specified, UTF-8 will be used.
To append string to beginning of list we have used [a] + l1 and the string will get appended to the list at the beginning. You can refer to the below screenshot to see the output for append string to beginning of list python.
Because even though they look like normal C
/ T
/ H
characters they are actually not those characters.
They are Cyrillic characters.
С - Cyrillic Capital letter ES
Т - Cyrillic Capital letter TE
Н - Cyrillic Capital letter EN
You would need to check wherever you got those characters from , on why they are so.
On why they got printed using the \x..
representation, is because when you print a list
, the __str__()
method on list is called, but list itself calls __repr__()
on its elements, and hence you get the internal representation of the strings. You would get similar result if you do -
print(repr(foo))
for the first case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With