如何避免在flask / python中使用UnicodeDecodeError?

时间:2014-03-19 12:02:57

标签: python

这个错误在我的烧瓶应用程序中惹恼了我。该烧瓶应该从Hbase数据库中获取表的列表并显示它们。但是一个特定的表显示

UnicodeDecodeError: 'ascii' codec can't decode byte 0xa1 in position 7: ordinal not in range(128)

当我打印表格内容时,我得到了这个

    [{'c:total': '\x00\x00\x00\x00\x00\x00\x00\x00'}, 
{'c:total': '\x00\x00\x00\x00\x00\x00\x00\x1b'},
 {'c:total': '\x00\x00\x00\x00\x00\x00\x084'}, {'c:total': '\x00\x00\x00\x00\x00\x00\x01\xa1'}, 
{'c:total': '\x00\x00\x00\x00\x00\x00\x00\xb9'}, {'c:total': '\x00\x00\x00\x00\x00\x00\x00\x00'}, 
{'c:total': '\x00\x00\x00\x00\x00\x00\x00\x01'}, {'c:total': '\x00\x00\x00\x00\x00\x05^\x84'},
 {'c:total': '\x00\x00\x00\x00\x00\x00\x01G'}, 
{'c:total': '\x00\x00\x00\x00\x00\x00\x00,'}]

我试过

sys.setdefaultencoding('utf-8')

然后错误变为

UnicodeDecodeError: 'utf8' codec can't decode byte 0xa1 in position 7: invalid start byte

这是追溯

Traceback (most recent call last):
  File "/usr/share/pyshared/flask/app.py", line 1701, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/share/pyshared/flask/app.py", line 1689, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/usr/share/pyshared/flask/app.py", line 1687, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/share/pyshared/flask/app.py", line 1360, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/share/pyshared/flask/app.py", line 1358, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/share/pyshared/flask/app.py", line 1344, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/nidhin/Hbase/run.py", line 28, in show_table
    return render_template('table.html', Tablename = tablename, tabledatas = tabledata, headers = tabledata[0])
  File "/usr/share/pyshared/flask/templating.py", line 125, in render_template
    context, ctx.app)
  File "/usr/share/pyshared/flask/templating.py", line 107, in _render
    rv = template.render(context)
  File "/usr/share/pyshared/jinja2/environment.py", line 894, in render
    return self.environment.handle_exception(exc_info, True)
  File "/home/nidhin/Hbase/templates/table.html", line 48, in top-level template code
    <td>{{ value }}</td>
UnicodeDecodeError: 'utf8' codec can't decode byte 0xa1 in position 7: invalid start byte

我的python代码

from flask import *
import sys
import happybase

reload(sys)
sys.setdefaultencoding('utf-8')
app = Flask(__name__)
connection = happybase.Connection('hbase.inzyte.com')


@app.route('/')
def home():
    tables = connection.tables()
    return render_template('tablist.html', tables = tables)


@app.route('/<tablename>')
def show_table(tablename=None):
    print "Entered"
    table = connection.table(tablename)
    print "Connected"
    tabledata = [data for key,data in table.scan()]
    print tabledata
    print "Data collected"
    return render_template('table.html', Tablename = tablename, tabledatas = tabledata, headers = tabledata[0])


app.run(debug = True)

这是显示它的html部分

  {% for data in tabledatas %}
  <tr>
    {% for value in data.values() %}
    <td>{{ value }}</td>
    {% endfor %}
  </tr>
  {% endfor %}

请帮帮我..

1 个答案:

答案 0 :(得分:0)

似乎'c:total'值是整数(作为字节数组),而不是字符串。尝试使用struct.unpack解码值

我的测试代码:

>>> tabledata = [{'c:total': '\x00\x00\x00\x00\x00\x00\x00\x00'}, {'c:total': '\x00\x00\x00\x00\x00\x00\x00\x1b'}, {'c:total': '\x00\x00\x00\x00\x00\x00\x084'}, {'c:total': '\x00\x00\x00\x00\x00\x00\x01\xa1'}, {'c:total': '\x00\x00\x00\x00\x00\x00\x00\xb9'}, {'c:total': '\x00\x00\x00\x00\x00\x00\x00\x00'}, {'c:total': '\x00\x00\x00\x00\x00\x00\x00\x01'}, {'c:total': '\x00\x00\x00\x00\x00\x05^\x84'}, {'c:total': '\x00\x00\x00\x00\x00\x00\x01G'}, {'c:total': '\x00\x00\x00\x00\x00\x00\x00,'}]
>>> import struct
>>> for row in tabledata:
        print struct.unpack('>Q', row['c:total'])[0]
0
27
2100
417
185
0
1
351876
327
44