UnicodeEncodeError仅在某些PC上

时间:2016-01-10 10:00:20

标签: python sqlite pandas

我有一个如下所示的示例代码,从sqlite数据库读取数据并将其导入到一些pandas数据帧中。

当我在桌面上运行它时,会打印所需的输出

Datenbank anlegen
    Datum     Name
0  160105  12351.3

但是,在我的笔记本电脑上运行它,我得到一个Unicode警告:

2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)]
Python Type "help", "copyright", "credits" or "license" for more information.
[evaluate 2016-01-10 Test PD.py]
C:\Python27\lib\pkgutil.py:380: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
  importer = sys.path_importer_cache[path_item]
Traceback (most recent call last):
  File "C:\Users\....\2016-01-10 Test PD.py", line 2, in <module>
    import pandas as pd
  File "C:\Python27\Lib\site-packages\pandas\__init__.py", line 13, in <module>
    from pandas import hashtable, tslib, lib
  File "C:\.....\pandas\tslib.pyx", line 61, in init pandas.tslib (pandas\tslib.c:103021)
  File "C:\Python27\Lib\site-packages\pytz\__init__.py", line 31, in <module>
    from pkg_resources import resource_stream
  File "C:\Python27\Lib\site-packages\pkg_resources.py", line 2829, in <module>
    working_set = WorkingSet._build_master()
  File "C:\Python27\Lib\site-packages\pkg_resources.py", line 440, in _build_master
    ws = cls()
  File "C:\Python27\Lib\site-packages\pkg_resources.py", line 433, in __init__
    self.add_entry(entry)
  File "C:\Python27\Lib\site-packages\pkg_resources.py", line 489, in add_entry
    for dist in find_distributions(entry, True):
  File "C:\Python27\Lib\site-packages\pkg_resources.py", line 1817, in find_distributions
    importer = get_importer(path_item)
  File "C:\Python27\Lib\pkgutil.py", line 387, in get_importer
    importer = path_hook(path_item)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 44: ordinal not in range(128)

当我使用调试器(wingide)逐步运行脚本时,脚本在笔记本电脑上运行时没有错误。

import sqlite3 as lite
import pandas as pd
import os


db_filename = "Test.db"

if not os.path.isfile(db_filename):
    con = lite.connect(db_filename)  
    print "Datenbank anlegen"
    try:
        with con:    
            cur = con.cursor()    
            cur.execute("CREATE TABLE Testtab(Datum TEXT, Name TEXT)")
            Datum= "160105"
            Name= "12351.3*ABC"
            cur.execute("INSERT INTO Testtab VALUES(?,?)",(Datum, Name))

    except lite.Error, e:
        print "Error 1: %s:" % e.args[0]
        sys.exit(1)      
con = lite.connect(db_filename)          

with con:    
    query = '''SELECT *
               FROM Testtab'''

    df = pd.read_sql(query, con)        


# DF konvertieren    
df['Name']=df['Name'].str[:-4].astype(float) 
print df
pandas和numpy在笔记本电脑上比较新(pandas 0.17.1而不是0.15.2,numpy 1.7.1而不是1.6.2)。

似乎数据作为unicode写入sqlite数据库,pd.read_sql不知何故不喜欢它。

我有点迷失在这里,任何帮助表示赞赏。

谢谢, 托马斯

1 个答案:

答案 0 :(得分:0)

似乎在添加

import sys
reload(sys)
sys.setdefaultencoding( "latin-1" ) 

解决了这个问题。不幸的是我无法正确解释原因,我只知道setdefaultencoding是默认情况下未加载的参数,这就是重载是必要的原因。