How to deal with error in pandas df to mysql ?

1,649 views
Skip to first unread message

Athrun Sun

unread,
Jul 5, 2016, 9:04:07 AM7/5/16
to PyData
hi, 

i am a chinese pythoner using pandas.

i am faced with a difficult problem now dealing with chinese word .
i have tried pymysql and sqlalchemy,nothing is ok.

please help me!

very very thanks!

win7 64, Anaconda python 2.7


 code:
1st wat: 
import pymysql

conn=None

conn = pymysql.connect(host='localhost', user='root', passwd='1234',db='chinese',use_unicode=True, charset='utf8')

conn.autocommit(1)

# there is a csv file  ,first column is chinese


ratio=pd.read_table('D:\python\occu\sanxia_cases_integrity_bymonth_season.csv',sep=',')

ratio.to_sql('test3',engine,index=False,if_exists='append')

error:
Execution failed on sql 'SELECT name FROM sqlite_master WHERE type='table' AND name=?;': not all arguments converted during string formatting

2 second way:
from sqlalchemy import create_engine
from sqlalchemy.types import VARCHAR
from sqlalchemy.types import String

engine = create_engine("mysql+pymysql://root:1234@localhost/chinese",encoding='utf8',convert_unicode=True)

ratio.to_sql('test1',engine,index=False,if_exists='append')

UnicodeEncodeError: 'latin-1' codec can't encode character u'\ufeff' in position 20: ordinal not in range(256)





Reply all
Reply to author
Forward
0 new messages