Python UnicodeDecodeError: 'utf8' codec can't decode byte 0x80 in position 74: invalid start byte
Python UnicodeDecodeError: 'utf8' codec can't decode byte 0x80 in position 74: invalid start byte
我在 hbase 中有一些数据存储为由 \x00 填充组合分隔的字节和字符串。
所以我的 hbase 中的行看起来像:-
00:00:00:00:00:00\x00\x80\x00\x00\x00U\xEF\xA0\xB00\x002\x0040.0.2.1\x00
这一行(key)对应的值是100
行说明:-
00:00:00:00:00:00 - This is mac address and is a string
\x80\x00\x00\x00U\xEF\xA0\xB00 - This is the time which is saved as bytes
2 - this is customer id number stored as string
40.0.2.1 - this is store ID stored as string
我已经使用 star base 模块将 python 连接到它的 stargate 服务器。
这是我连接到 starbase 和 hbase table 的代码片段,并尝试获取该行的值:-
from starbase import Connection
import starbase
C = Connection(host='10.10.5.2', port='60010')
get_table = C.table('dummy_table')
mac_address = "00:00:00:00:00:00"
time_start = "\x80\x00\x00\x00U\xEF\xA0\xB00"
cus_id = "2"
store_id = "40.0.2.1"
create_query = "%s\x00%s\x00%s\x00%s\x00" % (mac,time_start,cus_id,store_id)
fetch_result = get_table.fetch(create_query)
print fetch_result
预期输出为:-
100
您不必担心 starbase 连接及其方法。如果一切都是字符串,它们就可以正常工作,但现在由于时间被转换为字节,它给我错误:-
UnicodeDecodeError: 'utf8' codec can't decode byte 0x80 in position 74: invalid start byte
以防万一您需要在我打印时查看 create_query 的输出:-
00:00:1E:00:C8:36▒U▒v▒130.0.2.6
非常感谢您的帮助。谢谢
我的猜测是您的数据库不支持在这些字段中存储字节;也许你必须存储字符串。
一种方法是在将字节存储到数据库之前将其转换为 base64 字符串。例如:
>>> from base64 import b64encode, b64decode
>>> b64encode("\x80\x00\x00\x00U\xEF\xA0\xB00")
'gAAAAFXvoLAw'
>>> b64decode(_)
'\x80\x00\x00\x00U\xef\xa0\xb00'
试试这个
time_start = "\x80\x00\x00\x00U\xEF\xA0\xB00"
\x 是十六进制值的转义序列,
create_query = "%s\x00%s\x00%s\x00%s\x00" % (mac,time_start,cus_id,store_id)
正在将 time_start 转换为字符串。由于 x80 不是有效的 utf-8,它会引发错误。
我在 hbase 中有一些数据存储为由 \x00 填充组合分隔的字节和字符串。
所以我的 hbase 中的行看起来像:-
00:00:00:00:00:00\x00\x80\x00\x00\x00U\xEF\xA0\xB00\x002\x0040.0.2.1\x00
这一行(key)对应的值是100
行说明:-
00:00:00:00:00:00 - This is mac address and is a string
\x80\x00\x00\x00U\xEF\xA0\xB00 - This is the time which is saved as bytes
2 - this is customer id number stored as string
40.0.2.1 - this is store ID stored as string
我已经使用 star base 模块将 python 连接到它的 stargate 服务器。
这是我连接到 starbase 和 hbase table 的代码片段,并尝试获取该行的值:-
from starbase import Connection
import starbase
C = Connection(host='10.10.5.2', port='60010')
get_table = C.table('dummy_table')
mac_address = "00:00:00:00:00:00"
time_start = "\x80\x00\x00\x00U\xEF\xA0\xB00"
cus_id = "2"
store_id = "40.0.2.1"
create_query = "%s\x00%s\x00%s\x00%s\x00" % (mac,time_start,cus_id,store_id)
fetch_result = get_table.fetch(create_query)
print fetch_result
预期输出为:-
100
您不必担心 starbase 连接及其方法。如果一切都是字符串,它们就可以正常工作,但现在由于时间被转换为字节,它给我错误:-
UnicodeDecodeError: 'utf8' codec can't decode byte 0x80 in position 74: invalid start byte
以防万一您需要在我打印时查看 create_query 的输出:-
00:00:1E:00:C8:36▒U▒v▒130.0.2.6
非常感谢您的帮助。谢谢
我的猜测是您的数据库不支持在这些字段中存储字节;也许你必须存储字符串。
一种方法是在将字节存储到数据库之前将其转换为 base64 字符串。例如:
>>> from base64 import b64encode, b64decode
>>> b64encode("\x80\x00\x00\x00U\xEF\xA0\xB00")
'gAAAAFXvoLAw'
>>> b64decode(_)
'\x80\x00\x00\x00U\xef\xa0\xb00'
试试这个
time_start = "\x80\x00\x00\x00U\xEF\xA0\xB00"
\x 是十六进制值的转义序列,
create_query = "%s\x00%s\x00%s\x00%s\x00" % (mac,time_start,cus_id,store_id)
正在将 time_start 转换为字符串。由于 x80 不是有效的 utf-8,它会引发错误。