Вот три части подобных кода, первые два запуска штрафа, последний из которых не удается -Python 2.6 Ошибка печати в Юникоде?
# 1
print '%d) "%s" ' % (new_item_count, item[u'Title'].encode('utf-8')),
print '%s' % item[u'CurrencyID'],
print '%s !' % item[u'Value']
# 2
print '%d) "%s" %s%s !!' % (new_item_count,
item[u'Title'].encode('utf-8'),
item[u'CurrencyID'].encode('utf-8'),
item[u'Value'])
# 3
print '%d) "%s" %s%s !!!' % (new_item_count,
item[u'Title'].encode('utf-8'),
item[u'CurrencyID'],
item[u'Value'])
Output (обратите внимание, дифференцирующие восклицательные знаки в конце) -
37) "HP Pavilion Laptop/Intel® Core™ i3 Processor" USD 510.0 !
37) "HP Pavilion Laptop/Intel® Core™ i3 Processor" USD510.0 !!
'ascii' codec can't decode byte 0xc2 in position 29: ordinal not in range(128)
Этот находится под Python 2.6.5 (Ubuntu 10,04 пакет, такое же поведение как в 32-битной и 64-битной) -
$ python -V
Python 2.6.5
$ uname -a
Linux <> 2.6.39.1-x86_64-<> #1 SMP Tue Jun 21 10:04:20 EDT 2011 x86_64 GNU/Linux
Единственное объяснение, которое я могу вспомнить, что это Python б UG.
Не так ли?
Какова ценность 'item [u'CurrencyID ']'? Попробуйте распечатать его 'repr'. –
Попробуйте 'print ('% d)% s% s% s'% ...). Encode ('utf-8')' –