Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Python
Unicode error
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Steven D'Aprano, post: 4105445"] [i] Don't write bare excepts, always catch the error you want and nothing else. As you've written it, the result of encoding with latin-1 is thrown away, even if it succeeds. text = texts[i] # Don't hide errors here. try: text = text.encode('latin-1') except UnicodeEncodeError: try: text = text.encode('utf-8') except UnicodeEncodeError: text = ' ' do_something_with(text) Another thing you might consider is setting the error handler: text = text.encode('utf-8', errors='ignore') Other error handlers are 'strict' (the default), 'replace' and 'xmlcharrefreplace'.[/i][/i] [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Python
Unicode error
Top