Behaviour unicode string in python
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have seen this question I have doubts about how can I convert a var to unicode on running time ?
Is it right use unicode function ?
Are there other way to convert a string on running time ?
print(u'Camiu00f3n') # prints with right special char
name=unicode('Camiu00f3n')
print(name) # prints bad ===> Camiu00f3n
name.encode('latin1')
print(name.decode('latin1')) # prints bad ===> Camiu00f3n
encoded_id = u'abcdxc3x9f'
encoded_id.encode('latin1').decode('utf8')
print encoded_id.encode('latin1').decode('utf8') # prints right
I saw a lot of python unicode questions on stackoverflow but i can't understand this behaviour.
python python-3.x unicode
add a comment |
I have seen this question I have doubts about how can I convert a var to unicode on running time ?
Is it right use unicode function ?
Are there other way to convert a string on running time ?
print(u'Camiu00f3n') # prints with right special char
name=unicode('Camiu00f3n')
print(name) # prints bad ===> Camiu00f3n
name.encode('latin1')
print(name.decode('latin1')) # prints bad ===> Camiu00f3n
encoded_id = u'abcdxc3x9f'
encoded_id.encode('latin1').decode('utf8')
print encoded_id.encode('latin1').decode('utf8') # prints right
I saw a lot of python unicode questions on stackoverflow but i can't understand this behaviour.
python python-3.x unicode
What are you trying to do? What data are you trying to convert? Where is it from? What does "on running time" mean?
– Daniel Roseman
Jun 16 '15 at 11:39
uhhhh
escape sequences only work in Python unicode literals. If you have data with such escape sequences, you may well have JSON data instead, which uses the same syntax. If so, use a JSON parser for that data.
– Martijn Pieters♦
Jun 16 '15 at 11:41
You can ask Python to interpret such sequences with a special codec, but that is usually the wrong interpretation of your data. Please share a sample of your actual data so we can help you with that.
– Martijn Pieters♦
Jun 16 '15 at 11:42
add a comment |
I have seen this question I have doubts about how can I convert a var to unicode on running time ?
Is it right use unicode function ?
Are there other way to convert a string on running time ?
print(u'Camiu00f3n') # prints with right special char
name=unicode('Camiu00f3n')
print(name) # prints bad ===> Camiu00f3n
name.encode('latin1')
print(name.decode('latin1')) # prints bad ===> Camiu00f3n
encoded_id = u'abcdxc3x9f'
encoded_id.encode('latin1').decode('utf8')
print encoded_id.encode('latin1').decode('utf8') # prints right
I saw a lot of python unicode questions on stackoverflow but i can't understand this behaviour.
python python-3.x unicode
I have seen this question I have doubts about how can I convert a var to unicode on running time ?
Is it right use unicode function ?
Are there other way to convert a string on running time ?
print(u'Camiu00f3n') # prints with right special char
name=unicode('Camiu00f3n')
print(name) # prints bad ===> Camiu00f3n
name.encode('latin1')
print(name.decode('latin1')) # prints bad ===> Camiu00f3n
encoded_id = u'abcdxc3x9f'
encoded_id.encode('latin1').decode('utf8')
print encoded_id.encode('latin1').decode('utf8') # prints right
I saw a lot of python unicode questions on stackoverflow but i can't understand this behaviour.
python python-3.x unicode
python python-3.x unicode
edited Nov 23 '18 at 14:45
Kasrâmvd
79.7k1092130
79.7k1092130
asked Jun 16 '15 at 11:28
UlyarezUlyarez
5529
5529
What are you trying to do? What data are you trying to convert? Where is it from? What does "on running time" mean?
– Daniel Roseman
Jun 16 '15 at 11:39
uhhhh
escape sequences only work in Python unicode literals. If you have data with such escape sequences, you may well have JSON data instead, which uses the same syntax. If so, use a JSON parser for that data.
– Martijn Pieters♦
Jun 16 '15 at 11:41
You can ask Python to interpret such sequences with a special codec, but that is usually the wrong interpretation of your data. Please share a sample of your actual data so we can help you with that.
– Martijn Pieters♦
Jun 16 '15 at 11:42
add a comment |
What are you trying to do? What data are you trying to convert? Where is it from? What does "on running time" mean?
– Daniel Roseman
Jun 16 '15 at 11:39
uhhhh
escape sequences only work in Python unicode literals. If you have data with such escape sequences, you may well have JSON data instead, which uses the same syntax. If so, use a JSON parser for that data.
– Martijn Pieters♦
Jun 16 '15 at 11:41
You can ask Python to interpret such sequences with a special codec, but that is usually the wrong interpretation of your data. Please share a sample of your actual data so we can help you with that.
– Martijn Pieters♦
Jun 16 '15 at 11:42
What are you trying to do? What data are you trying to convert? Where is it from? What does "on running time" mean?
– Daniel Roseman
Jun 16 '15 at 11:39
What are you trying to do? What data are you trying to convert? Where is it from? What does "on running time" mean?
– Daniel Roseman
Jun 16 '15 at 11:39
uhhhh
escape sequences only work in Python unicode literals. If you have data with such escape sequences, you may well have JSON data instead, which uses the same syntax. If so, use a JSON parser for that data.– Martijn Pieters♦
Jun 16 '15 at 11:41
uhhhh
escape sequences only work in Python unicode literals. If you have data with such escape sequences, you may well have JSON data instead, which uses the same syntax. If so, use a JSON parser for that data.– Martijn Pieters♦
Jun 16 '15 at 11:41
You can ask Python to interpret such sequences with a special codec, but that is usually the wrong interpretation of your data. Please share a sample of your actual data so we can help you with that.
– Martijn Pieters♦
Jun 16 '15 at 11:42
You can ask Python to interpret such sequences with a special codec, but that is usually the wrong interpretation of your data. Please share a sample of your actual data so we can help you with that.
– Martijn Pieters♦
Jun 16 '15 at 11:42
add a comment |
1 Answer
1
active
oldest
votes
Its just because of that if you don't specify any encoding for unicode
function then :
unicode() will mimic the behaviour of str() except that it returns Unicode strings instead of 8-bit strings. More precisely, if object is a Unicode string or subclass it will return that Unicode string without any additional decoding applied.
So you'll have a str
version of your unicode (the Unicode part will be escaped):
>>> name=unicode('Camiu00f3n')
>>> print(name)
Camiu00f3n
>>> name
u'Cami\u00f3n'
^
For get ride of this problem you can use 'unicode-escape'
as your encoding to escape converting the Unicode to string!
>>> name=unicode('Camiu00f3n','unicode-escape')
>>> name
u'Camixf3n'
>>> print(name)
Camión
Works like a charm. I tried to use it to print data from a database with special characters for code tests. Thanks!
– Ulyarez
Jun 16 '15 at 11:56
1
Note thatunicode-escape
interprets more than just theuhhhh
escapes. If there are other `` backslash-escapes in the text, those too will be interpreted, and may not be what you expected.
– Martijn Pieters♦
Jun 16 '15 at 12:16
@Ulyarez Welcome, also note about Martijn's comment!
– Kasrâmvd
Jun 16 '15 at 12:24
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f30866280%2fbehaviour-unicode-string-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Its just because of that if you don't specify any encoding for unicode
function then :
unicode() will mimic the behaviour of str() except that it returns Unicode strings instead of 8-bit strings. More precisely, if object is a Unicode string or subclass it will return that Unicode string without any additional decoding applied.
So you'll have a str
version of your unicode (the Unicode part will be escaped):
>>> name=unicode('Camiu00f3n')
>>> print(name)
Camiu00f3n
>>> name
u'Cami\u00f3n'
^
For get ride of this problem you can use 'unicode-escape'
as your encoding to escape converting the Unicode to string!
>>> name=unicode('Camiu00f3n','unicode-escape')
>>> name
u'Camixf3n'
>>> print(name)
Camión
Works like a charm. I tried to use it to print data from a database with special characters for code tests. Thanks!
– Ulyarez
Jun 16 '15 at 11:56
1
Note thatunicode-escape
interprets more than just theuhhhh
escapes. If there are other `` backslash-escapes in the text, those too will be interpreted, and may not be what you expected.
– Martijn Pieters♦
Jun 16 '15 at 12:16
@Ulyarez Welcome, also note about Martijn's comment!
– Kasrâmvd
Jun 16 '15 at 12:24
add a comment |
Its just because of that if you don't specify any encoding for unicode
function then :
unicode() will mimic the behaviour of str() except that it returns Unicode strings instead of 8-bit strings. More precisely, if object is a Unicode string or subclass it will return that Unicode string without any additional decoding applied.
So you'll have a str
version of your unicode (the Unicode part will be escaped):
>>> name=unicode('Camiu00f3n')
>>> print(name)
Camiu00f3n
>>> name
u'Cami\u00f3n'
^
For get ride of this problem you can use 'unicode-escape'
as your encoding to escape converting the Unicode to string!
>>> name=unicode('Camiu00f3n','unicode-escape')
>>> name
u'Camixf3n'
>>> print(name)
Camión
Works like a charm. I tried to use it to print data from a database with special characters for code tests. Thanks!
– Ulyarez
Jun 16 '15 at 11:56
1
Note thatunicode-escape
interprets more than just theuhhhh
escapes. If there are other `` backslash-escapes in the text, those too will be interpreted, and may not be what you expected.
– Martijn Pieters♦
Jun 16 '15 at 12:16
@Ulyarez Welcome, also note about Martijn's comment!
– Kasrâmvd
Jun 16 '15 at 12:24
add a comment |
Its just because of that if you don't specify any encoding for unicode
function then :
unicode() will mimic the behaviour of str() except that it returns Unicode strings instead of 8-bit strings. More precisely, if object is a Unicode string or subclass it will return that Unicode string without any additional decoding applied.
So you'll have a str
version of your unicode (the Unicode part will be escaped):
>>> name=unicode('Camiu00f3n')
>>> print(name)
Camiu00f3n
>>> name
u'Cami\u00f3n'
^
For get ride of this problem you can use 'unicode-escape'
as your encoding to escape converting the Unicode to string!
>>> name=unicode('Camiu00f3n','unicode-escape')
>>> name
u'Camixf3n'
>>> print(name)
Camión
Its just because of that if you don't specify any encoding for unicode
function then :
unicode() will mimic the behaviour of str() except that it returns Unicode strings instead of 8-bit strings. More precisely, if object is a Unicode string or subclass it will return that Unicode string without any additional decoding applied.
So you'll have a str
version of your unicode (the Unicode part will be escaped):
>>> name=unicode('Camiu00f3n')
>>> print(name)
Camiu00f3n
>>> name
u'Cami\u00f3n'
^
For get ride of this problem you can use 'unicode-escape'
as your encoding to escape converting the Unicode to string!
>>> name=unicode('Camiu00f3n','unicode-escape')
>>> name
u'Camixf3n'
>>> print(name)
Camión
answered Jun 16 '15 at 11:44
KasrâmvdKasrâmvd
79.7k1092130
79.7k1092130
Works like a charm. I tried to use it to print data from a database with special characters for code tests. Thanks!
– Ulyarez
Jun 16 '15 at 11:56
1
Note thatunicode-escape
interprets more than just theuhhhh
escapes. If there are other `` backslash-escapes in the text, those too will be interpreted, and may not be what you expected.
– Martijn Pieters♦
Jun 16 '15 at 12:16
@Ulyarez Welcome, also note about Martijn's comment!
– Kasrâmvd
Jun 16 '15 at 12:24
add a comment |
Works like a charm. I tried to use it to print data from a database with special characters for code tests. Thanks!
– Ulyarez
Jun 16 '15 at 11:56
1
Note thatunicode-escape
interprets more than just theuhhhh
escapes. If there are other `` backslash-escapes in the text, those too will be interpreted, and may not be what you expected.
– Martijn Pieters♦
Jun 16 '15 at 12:16
@Ulyarez Welcome, also note about Martijn's comment!
– Kasrâmvd
Jun 16 '15 at 12:24
Works like a charm. I tried to use it to print data from a database with special characters for code tests. Thanks!
– Ulyarez
Jun 16 '15 at 11:56
Works like a charm. I tried to use it to print data from a database with special characters for code tests. Thanks!
– Ulyarez
Jun 16 '15 at 11:56
1
1
Note that
unicode-escape
interprets more than just the uhhhh
escapes. If there are other `` backslash-escapes in the text, those too will be interpreted, and may not be what you expected.– Martijn Pieters♦
Jun 16 '15 at 12:16
Note that
unicode-escape
interprets more than just the uhhhh
escapes. If there are other `` backslash-escapes in the text, those too will be interpreted, and may not be what you expected.– Martijn Pieters♦
Jun 16 '15 at 12:16
@Ulyarez Welcome, also note about Martijn's comment!
– Kasrâmvd
Jun 16 '15 at 12:24
@Ulyarez Welcome, also note about Martijn's comment!
– Kasrâmvd
Jun 16 '15 at 12:24
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f30866280%2fbehaviour-unicode-string-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What are you trying to do? What data are you trying to convert? Where is it from? What does "on running time" mean?
– Daniel Roseman
Jun 16 '15 at 11:39
uhhhh
escape sequences only work in Python unicode literals. If you have data with such escape sequences, you may well have JSON data instead, which uses the same syntax. If so, use a JSON parser for that data.– Martijn Pieters♦
Jun 16 '15 at 11:41
You can ask Python to interpret such sequences with a special codec, but that is usually the wrong interpretation of your data. Please share a sample of your actual data so we can help you with that.
– Martijn Pieters♦
Jun 16 '15 at 11:42