google protocol buffer在python中使用utf-8的问题
2008-12-25 18:04
691 查看
google protocol buffer好用,但是在python中会有一些小问题。比如不支持utf-8只支持unicode的字符串。在我们的系统中,存储的和传输的都是utf-8,因此在代码中(c++,java,python)都统一使用utf-8格式。所以就带来不方便,比如c++中需要从utf-8转为unicode,python中再解析出来。实际上,可以让google protocol buffer for python不进行转码。
我遇到的问题是:c++读取文件(文件时utf-8的),然后python接受,c++发送的是utf-8的,但是python却要求unicode的,巨郁闷。
对于string类型的描述如下,说utf-8和ascii都支持,实际上python版本的却要求unicode,实在是可笑,由此可见,实际上试一下比说啥都强。
下载google protocol buffer的源码,如http://code.google.com/p/protobuf/downloads/list,下载2.0.3版本。
进入/protobuf-2.0.3/python/google/protobuf/internal目录下
encode.py
def AppendString(self, field_number, value):
"""Appends a length-prefixed unicode string, encoded as UTF-8 to our buffer,
with the length varint-encoded.
"""
# self.AppendBytes(field_number, value.encode('utf-8'))
# 将value.encode去掉,我本来就是utf-8,干吗还编码呢
self.AppendBytes(field_number, value)
decode.py
def ReadString(self):
"""Reads and returns a length-delimited string."""
bytes = self.ReadBytes()
# return unicode(bytes, 'utf-8')
# 改为下面这句,啥也不干,是什么编码就返回什么编码
return bytes
type_check.py
class UnicodeValueChecker(object):
"""Checker used for string fields."""
def CheckValue(self, proposed_value):
if not isinstance(proposed_value, (str, unicode)):
message = ('%.1024r has type %s, but expected one of: %s' %
(proposed_value, type(proposed_value), (str, unicode)))
raise TypeError(message)
# If the value is of type 'str' make sure that it is in 7-bit ASCII
# encoding.
# 莫名其妙,为何要try convert to unicode,而且是从ascii转,注释掉
# if isinstance(proposed_value, str):
# try:
# unicode(proposed_value, 'ascii')
# except UnicodeDecodeError:
# raise ValueError('%.1024r isn/'t in 7-bit ASCII encoding.'
# % (proposed_value))
改了之后,重新安装一下,它就不会给你转编码了,你想传什么编码就什么编码。我用过c++版本的,好像不会处理编码问题。不过好像proto的数据类型中bytes也能满足要求,bytes它是不做任何处理的。
我遇到的问题是:c++读取文件(文件时utf-8的),然后python接受,c++发送的是utf-8的,但是python却要求unicode的,巨郁闷。
对于string类型的描述如下,说utf-8和ascii都支持,实际上python版本的却要求unicode,实在是可笑,由此可见,实际上试一下比说啥都强。
string | A string must always contain UTF-8 encoded or 7-bit ASCII text. | string | String |
进入/protobuf-2.0.3/python/google/protobuf/internal目录下
encode.py
def AppendString(self, field_number, value):
"""Appends a length-prefixed unicode string, encoded as UTF-8 to our buffer,
with the length varint-encoded.
"""
# self.AppendBytes(field_number, value.encode('utf-8'))
# 将value.encode去掉,我本来就是utf-8,干吗还编码呢
self.AppendBytes(field_number, value)
decode.py
def ReadString(self):
"""Reads and returns a length-delimited string."""
bytes = self.ReadBytes()
# return unicode(bytes, 'utf-8')
# 改为下面这句,啥也不干,是什么编码就返回什么编码
return bytes
type_check.py
class UnicodeValueChecker(object):
"""Checker used for string fields."""
def CheckValue(self, proposed_value):
if not isinstance(proposed_value, (str, unicode)):
message = ('%.1024r has type %s, but expected one of: %s' %
(proposed_value, type(proposed_value), (str, unicode)))
raise TypeError(message)
# If the value is of type 'str' make sure that it is in 7-bit ASCII
# encoding.
# 莫名其妙,为何要try convert to unicode,而且是从ascii转,注释掉
# if isinstance(proposed_value, str):
# try:
# unicode(proposed_value, 'ascii')
# except UnicodeDecodeError:
# raise ValueError('%.1024r isn/'t in 7-bit ASCII encoding.'
# % (proposed_value))
改了之后,重新安装一下,它就不会给你转编码了,你想传什么编码就什么编码。我用过c++版本的,好像不会处理编码问题。不过好像proto的数据类型中bytes也能满足要求,bytes它是不做任何处理的。
相关文章推荐
- 在python中使用google protocol buffer
- google protocol buffer的字符编码问题(c++/java/python)
- Google Protocol Buffer 的使用和原理
- google protocol buffer的原理和使用(二)
- Google Protocol Buffer 的使用和原理(转载)
- Google Protocol Buffer 的使用和原理
- Google Protocol Buffer 的使用和原理
- Google Protocol Buffer 的使用和原理
- Google Protocol Buffer 的使用(未完待续)
- Google Protocol Buffer 的使用和原理(zt)
- Google Protocol Buffer 的使用和原理 .
- Google protocol buffer文件的原理和使用
- Google Protocol Buffer 的使用和原理
- Google Protocol Buffer 的使用和原理
- Google Protocol Buffer 的使用和原理
- Google Protocol Buffer 的使用和原理(转)
- google protocol buffer的原理和使用(一)
- google protocol buffer的原理和使用(三)
- google protocol buffer的原理和使用(四)
- Google Protocol Buffer 的使用和原理(转)