Forums

charset problem

So, Im trying to use strings encoded in cp1251. On local machine with linux it's ok, but on pythonanywhere's server it says:

>> print str(data[1]).decode('utf-8')
>> UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-7: ordinal not in range(128)

What's wrong?

vcabba

Could you provide a sample string that returns this error to you?

Found solution.... For example:

1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 import feedparser
5
6 rssData = feedparser.parse('http://lenta.ru/rss')
7
8 for data in rssData.entries:
9     print "%s".decode('utf8') % data.title
10     print str(data.title).decode('utf8')

variant like (9) ok

but variant like (10) gives error

now I think that is because of str() function, that tries to decode string to ascii by default

I'm glad it's resolved!

There's a bit of discussion about the underlying problem in this article.

>>> import sys
>>> sys.getdefaultencoding()
'ascii'

The default encoding on PythonAnywhere is 'ascii'. Maybe you have it set to something else on your local machine?

Out of curiosity...Why 'ascii' versus 'utf-8'?

That is if you don't mind answering.

Then again you are using 'utf-8' on Py3x and that is why I was really wondering.

Thus I guess I'm not wondering anything anymore and probably shouldn't hit Add post.

ASCII is Python's default encoding, unless the environment specifies otherwise. It's probably not a good idea for PA to impose a different default to the default default (!) because that has the potential to break code which wasn't written with i18n in mind.

Code which was written with i18n in mind shouldn't depend on the default encoding, it should always explicitly encode all external input to unicode objects by making a concious choice about the encoding, and such code will run fine regardless of the default encoding.

Hence, defaulting to ASCII is the best option for services that want to remain compatible. Also see this SO question.

Of course, Python 3 makes all this better, so once everyone's adopted that then life will be good. At the present rate of adoption that might even happen before my 6-month-old daughter graduates from university (but I wouldn't bet on it).

Well, then you'll be happy to know I use py3k exclusively...☺

It's is totally time for me to at least move my personal projects to Python3...

That's the spirit!! One at a time we'll get there...☺