Odoo Python 2.7 编码 utf8
Odoo Python 2.7 Encoding utf8
我正在尝试从几个字段中创建一个 unicode 字符串。我愿意:
my_unicode_text = u' '.join((u'ID ',str(order_line.product_id.id),u' | [',order_line.product_id.code,u'] ',str(order_line.product_id.name),u' | ',str(order_line.product_uom_qty),u'<br/>')).encode('utf-8').strip())
但我得到:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xd3' in position 18: ordinal not in range(128)
我想我没有正确使用 join / str。
也许如果您将连接替换为以下格式:
class Product_id:
def __init__(self, id, code, name):
self.id, self.code, self.name = id, code, name
class Order_line:
def __init__(self, product_id, product_uom_qty):
self.product_id, self.product_uom_qty = product_id, product_uom_qty
order_line = Order_line(Product_id(42, 6789, u'\N{greek small letter beta}eer'), 6)
s = unicode.format(u'ID {order_line.product_id.id} | [{product_id.code}] {product_id.name} | {order_line.product_uom_qty}<br/>', order_line=order_line, product_id=order_line.product_id)
print s
encode
用于从 unicode
到 str
而不是相反,意味着将 unicode
转换为 bytes
(str
在python 2.x):
# -*- coding: utf-8 -*-
s = u'aller â'
print(type(s))
print(type(s.encode('utf-8'))) # convert to str but using utf-8 codec
print(s + str(1)) # unicode + str == unicode
print(str(s)) # generate an exception because its using `ascii` codec by default
因此在您的示例产品名称中已准备就绪 utf-8
unicode
根本不需要转换它,只需转换非字符串对象,如整数、浮点数。
my_unicode_text = u' '.join(
(u'ID ',
str(order_line.product_id.id), # Integer
u' | [',
order_line.product_id.code,
u'] ',
order_line.product_id.name, # utf-8 uncide (database encoding)
u' | ',
str(order_line.product_uom_qty), # floats
u'<br/>'))
但不使用连接,而是使用字符串格式并手动转换值:
my_unicode_text = u'ID {order_line.product_id.id} | [{order_line.product_id.code}] ' \
u'{order_line.product_id.name} | {order_line.product_uom_qty}<br/>'.format(order_line=order_line)
我正在尝试从几个字段中创建一个 unicode 字符串。我愿意:
my_unicode_text = u' '.join((u'ID ',str(order_line.product_id.id),u' | [',order_line.product_id.code,u'] ',str(order_line.product_id.name),u' | ',str(order_line.product_uom_qty),u'<br/>')).encode('utf-8').strip())
但我得到:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xd3' in position 18: ordinal not in range(128)
我想我没有正确使用 join / str。
也许如果您将连接替换为以下格式:
class Product_id:
def __init__(self, id, code, name):
self.id, self.code, self.name = id, code, name
class Order_line:
def __init__(self, product_id, product_uom_qty):
self.product_id, self.product_uom_qty = product_id, product_uom_qty
order_line = Order_line(Product_id(42, 6789, u'\N{greek small letter beta}eer'), 6)
s = unicode.format(u'ID {order_line.product_id.id} | [{product_id.code}] {product_id.name} | {order_line.product_uom_qty}<br/>', order_line=order_line, product_id=order_line.product_id)
print s
encode
用于从 unicode
到 str
而不是相反,意味着将 unicode
转换为 bytes
(str
在python 2.x):
# -*- coding: utf-8 -*-
s = u'aller â'
print(type(s))
print(type(s.encode('utf-8'))) # convert to str but using utf-8 codec
print(s + str(1)) # unicode + str == unicode
print(str(s)) # generate an exception because its using `ascii` codec by default
因此在您的示例产品名称中已准备就绪 utf-8
unicode
根本不需要转换它,只需转换非字符串对象,如整数、浮点数。
my_unicode_text = u' '.join(
(u'ID ',
str(order_line.product_id.id), # Integer
u' | [',
order_line.product_id.code,
u'] ',
order_line.product_id.name, # utf-8 uncide (database encoding)
u' | ',
str(order_line.product_uom_qty), # floats
u'<br/>'))
但不使用连接,而是使用字符串格式并手动转换值:
my_unicode_text = u'ID {order_line.product_id.id} | [{order_line.product_id.code}] ' \
u'{order_line.product_id.name} | {order_line.product_uom_qty}<br/>'.format(order_line=order_line)