Questions tagged [python-unicode]

Python distinguishes between byte strings and unicode strings. *Decoding* transforms bytestrings to unicode; *encoding* transform unicode strings to bytes.

Python distinguishes between byte strings and unicode strings. Decoding transforms bytestrings to unicode; encoding transform unicode strings to bytes.

Remember: you decode your input to unicode, work with unicode, then encode unicode objects for output as bytes.

See the

1053 questions
0
votes
3 answers

Why don't python interpreter use the file coding format for decoding?

The code bellow will cause an UnicodeDecodeError: #-*- coding:utf-8 -*- s="中文" u=u"123" u=s+u I know it's because python interpreter is using ascii to decode s. Why…
WKPlus
  • 6,955
  • 2
  • 35
  • 53
0
votes
1 answer

UnicodeDecodeError with the sys.stdout inside traceback.print_exc()

I am getting UnicodeDecodeError with the traceback.print_exc(file=sys.stdout). I am using Python3.4 and did not get the problem with Python2.7. Am I missing something here? How can I make sure that sys.stdout passes the correct encoded/decoded to…
sk11
  • 1,779
  • 1
  • 17
  • 29
0
votes
1 answer

Simple list-dict funct

I'm trying to code a simple program in order to simulate a simple bus line with some station to exchange. I used 2 list for the bus stop and a dict for the line:bus-stop Here is the code `def main(): lineA = ["Stazione Autolinee","Via…
0
votes
2 answers

Python: UnicodeEncodeError: 'ascii' codec can't encode characters in position 34-39: ordinal not in range(128)

I've got a data of twitter log and I have to sort the file to show each user's retweeted tweet ranking. Here's the code. import codecs with codecs.open('hoge_qdata.tsv', 'r', 'utf-8') as tweets: tweet_list =…
user3816980
  • 1
  • 1
  • 2
0
votes
1 answer

Hashlib unicode error

I am trying to hash the password and not able to succeed. this is the code. from hashlib import sha1 as sha_constructor import random def generate_sha1(string, salt=None): if not isinstance(string, (str, str)): string = str(string) …
ajknzhol
  • 6,322
  • 13
  • 45
  • 72
0
votes
0 answers

'ascii' codec can't encode character

I am trying to parse an HTML link into the code and take its source code as list of strings. As I have to use get some relevant data from it, I am decoding everything into UTF-8 scheme. I am also using beautifulsoup4 which extracts the text in…
Prashant Shrivastava
  • 681
  • 1
  • 11
  • 19
0
votes
2 answers

UnicodeEncodeError: 'ascii' codec can't encode characters due to één from database

I have a field to get from database which contains string with this part één and while getting this i get error: "UnicodeEncodeError: 'ascii' codec can't encode characters in position 12-15: ordinal not in range(128)" I have search this error,…
0
votes
1 answer

Unable to save string into variable

I work with HP uCMDB to extract data from servers. In my python script I have this: iostat_cmd = client.executeCmd('iostat -En '+disk+'|egrep \'Vendor|Size\'') Which executes iostat and returns this: -bash-3.2$ iostat -En|egrep…
Xvs
  • 79
  • 2
  • 9
0
votes
2 answers

How to convert a unicode object to a string or a python dictionary

As a result of an API call I get the following object of : {"From":"en","Translations":[{"Count":0,"MatchDegree":100,"MatchedOriginalText":"","Rating":5,"TranslatedText":"Cómo estás"}]} but when I try to parse it with…
laurids
  • 931
  • 9
  • 24
0
votes
0 answers

UnicodeDecodeError even after setting `errors` to `ignore`

There are UnicodeDecodeError even after I set the errors attribute of StreamWriter to ignore. Here is the code: import codecs import platform if platform.system() == 'Linux': writer = codecs.getwriter('utf8') sys.stdout = writer(sys.stdout)…
Shuai Zhang
  • 2,011
  • 3
  • 22
  • 23
0
votes
1 answer

When setting default encoding to UTF-8 in Google App Engine, do I have to set it in all python files?

I got this code in this answer: Set Python27 Google AppEngine default encoding for entire app #!/usr/bin/python # -*- coding: utf-8 -*- from __future__ import unicode_literals Do I have to do this just in main.py (file with request handlers) or to…
Albert
  • 3,611
  • 3
  • 28
  • 52
0
votes
1 answer

Print without unicode symbol, but keep as unicode

I need to print values that have non ascii symbols. These values are unicode strings. So I have this list: some_list = [u'Data', u'Svoris', u'Perdavimo laikas', u'\u012evykio vietos adresas', u'Kvietimo prie\u017eastis'] I can print it with…
Andrius
  • 19,658
  • 37
  • 143
  • 243
0
votes
0 answers

Safely convert objects to strings without getting unicode errors?

If I have an unknown object X with some method of getting a string representation (__unicode__, __str__, __repr__, etc.). How can I safely convert that object to a unicode string without ever having to worry about seeing any of those ugly unicode…
TheOne
  • 10,819
  • 20
  • 81
  • 119
0
votes
2 answers

Not sure why Python Try-Except is not working

My Python program has been getting a UnicodeDecodeError, so I thought I could use try-except in my code to bypass it. However, even with the try-except, I continue to get the UnicodeDecodeError and my program simply refuses to run. Am I using…
Shuklaswag
  • 1,003
  • 1
  • 10
  • 27
0
votes
1 answer

In Python how to encode/decode unicode characters such as ö

Using Python 2.6.6 on CentOS 6.4 import json import urllib2 url = 'http://www.google.com.hk/complete/search?output=toolbar&hl=en&q=how%20to%20pronounce%20e' opener = urllib2.build_opener(urllib2.HTTPCookieProcessor()) opener.addheaders =…
davidjhp
  • 7,816
  • 9
  • 36
  • 56