python的新手,我想我已经打到了unicode课程墙。 我的筹码:
Python 3.4.3 virtualenv。 我正在开发一个小型的应用程序,允许用户上传excel工作簿,然后应用程序将行写入oracle db。当我使用python 2.7.10时,我开始使用此错误代码。我读到的大部分内容都表明它是由于我对python 2.X的默认解释器编码。所以我把代码移到了python 3.4.3虚拟环境中。这个错误仍在发生。
在下面的代码中,我正在使用要插入行的单元格类型来压缩列类型。创建字典列表后,调用sqlalchemy进行插入。执行调用executemany。
当它遇到excel工作表中的特殊字符,如版权,注册商标标志等时,会发生此错误。下面的if条件导致了麻烦。由于这些特殊字符是文本字符串的一部分,我希望python 3将其视为字符串,然后插入它。
从我的所有阅读材料来看,python 3默认情况下应该使用utf-8编码。我已经阅读了我使用的所有软件包,它们都使用utf-8编码。所以我不明白为什么它试图使用ascii来编码这个字符串&因此这个错误。我试过在值上做一个显式的.encode('utf-8')但是当excel单元格类型为'None'时会导致另一个问题。这意味着它是一个空单元格。然后我得到Nonetype没有编码属性错误。
output_data = []
for row in allrows:
row_dict = {}
for fobject,cobject in zip(sortedtablecolumnsobject,row):
########################################################
##Validation & assignment
########################################################
if fobject.type.name == "Text":
row_dict[fobject.name] = str(cobject.value)
output_data.append(row_dict)
这是表反射和放大的地方。插入发生:
m = db.MetaData()
t = db.Table(tableinfo.table_name,m,autoload = True, autoload_with = db.engine)
db.engine.execute(t.insert(),output_data)
我认为其中一个软件包在未经我同意的情况下使用ascii编码:-)请帮忙。
修改:
>import sys, locale
>sys.stdout.encoding
>'UTF-8'
>locale.getpreferredencoding(False)
>'UTF-8'
下面的跟踪:
Traceback (most recent call last):
File "/home/vagrant/Development/dutmyway/env/lib/python3.4/site-packages/flask /app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/home/vagrant/Development/dutmyway/env/lib/python3.4/site-packages/flask /app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/home/vagrant/Development/dutmyway/env/lib/python3.4/site-packages/flask /app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/vagrant/Development/dutmyway/env/lib/python3.4/site-packages/flask /_compat.py", line 33, in reraise
raise value
File "/home/vagrant/Development/dutmyway/env/lib/python3.4/site-packages/flask /app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/home/vagrant/Development/dutmyway/env/lib/python3.4/site-packages/flask /app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/vagrant/Development/dutmyway/env/lib/python3.4/site-packages/flask /app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/vagrant/Development/dutmyway/env/lib/python3.4/site-packages/flask /_compat.py", line 33, in reraise
raise value
File "/home/vagrant/Development/dutmyway/env/lib/python3.4/site-packages/flask /app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/home/vagrant/Development/dutmyway/env/lib/python3.4/site-packages/flask /app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/vagrant/Development/dutmyway/app/views.py", line 220, in upload
db.engine.execute(t.insert(),output_data)
File "/home/vagrant/Development/dutmyway/env/lib/python3.4/site-packages/sqlal chemy/engine/base.py", line 1991, in execute
return connection.execute(statement, *multiparams, **params)
File "/home/vagrant/Development/dutmyway/env/lib/python3.4/site-packages/sqlal chemy/engine/base.py", line 914, in execute
return meth(self, multiparams, params)
File "/home/vagrant/Development/dutmyway/env/lib/python3.4/site-packages/sqlal chemy/sql/elements.py", line 323, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/home/vagrant/Development/dutmyway/env/lib/python3.4/site-packages/sqlal chemy/engine/base.py", line 1010, in _execute_clauseelement
compiled_sql, distilled_params
File "/home/vagrant/Development/dutmyway/env/lib/python3.4/site-packages/sqlal chemy/engine/base.py", line 1146, in _execute_context
context)
File "/home/vagrant/Development/dutmyway/env/lib/python3.4/site-packages/sqlal chemy/engine/base.py", line 1344, in _handle_dbapi_exception
util.reraise(*exc_info)
File "/home/vagrant/Development/dutmyway/env/lib/python3.4/site-packages/sqlal chemy/util/compat.py", line 182, in reraise
raise value
File "/home/vagrant/Development/dutmyway/env/lib/python3.4/site-packages/sqlal chemy/engine/base.py", line 1116, in _execute_context
context)
File "/home/vagrant/Development/dutmyway/env/lib/python3.4/site-packages/sqlal chemy/dialects/oracle/cx_oracle.py", line 961, in do_executemany
cursor.executemany(statement, parameters)