I want to write some simple Python scripts that can be used unmodified on different Python versions, but I'm having trouble with strings...
text = get_data() phrases = [ "Soggarth Eogham O'Growney ,克尔・德怀尔", "capitis #3 病态上升涨大的繁殖性勃现", "IsoldeIsult、第一任威尔士亲王" ] for item in phrases: if item not in text: **# 3.3 ok. 2.7 UnicodeDecodeError** print ("Expected phrase '" + item + "' not found") The code above works in 3.3. When I try to run it under 2.7 I get
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 27: ordinal not in range(128) This is easily fixed by changing the first line to
text = get_data().encode('utf-8') But then, this does not work on 3.3. Any way to make this work with one version of the source code? Python noob.
sys.version_info.majorand only callencode()when it's less than3.phrases = [ u"Soggarth Eogham O'Growney ,克尔・德怀尔", u"capitis #3 病态上升涨大的繁殖性勃现", u"IsoldeIsult、第一任威尔士亲王" ].