dimanche 10 janvier 2016

UnicodeEncodeError only on some PCs

I have a sample code as listed below, reading data from a sqlite database and importing it into some pandas dataframe.

When I run it on my desktop, it prints the desired output

Datenbank anlegen
    Datum     Name
0  160105  12351.3

However, running it on my laptop, I get a Unicode Warning:

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)

The script runs through without errors on the laptop when I run through it step by step with my debugger (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 and numpy are newer on the laptop (pandas 0.17.1 instead of 0.15.2, numpy 1.7.1 instead of 1.6.2).

It seems that the data is written into the sqlite database as unicode and the pd.read_sql somehow does not like it.

I am a little bit lost here, any help appreciated.

Thanks, Thomas

Aucun commentaire:

Enregistrer un commentaire