amazon web services - Python encoding error? UnicodeDecodeError: 'ascii' codec can't decode byte ordinal not in range(128) -


i have aws autoscale instance. on ami instance, have file myfile.py contains following string:

x5zΠ

in aws cloudformation, launchconfiguration, have userdata executes following python instruction when instance spun up. reads myfile.py , attempts replace occurrences of regexp "x\dz" "xyz":

mystring = "xyz".join(re.compile('x\d\z').split(open("myfile.py", "r").read())) 

that produces error:

unicodedecodeerror: 'ascii' codec can't decode byte 0xc5 in position 4: ordinal not in range(128) 

okay. understand character Πcausing trouble because non-ascii. when manually run same statement python shell, works fine without throwing exception.

how can force script run aws launchconfig's userdata have same behavior when run manually myself? encoding should set , how should set it?

your file encoded, means represents unicode in defined way. use literal strings (e.g. xyz), shall used in oprations file content. these literal strings encoded file, in code defined.

python tries coerce both same able operate on it. best, if convert both unicode. string literals, prepend u, so: u"xyz". file have tell python encoding, if don't, default assumes ascii. try:

 mystring = u"xyz".join(re.compile(u'x\d\z').split(      open("myfile.py", "r").read().decode('utf-8'))) 

in version used utf-8, has probability of being right. if know being different, you'd have substitute right one (your editor in saved file can tell you).

edit: removed part console , environment settings, don't apply, @martijn mentioned. he's right about, error reproducible if string literals are implicitly unicode (from __future__ import unicode_literals) - in case there's no need prepend string literals u.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -