我有一个csv文件,我通过熊猫读取。然后我试图对该文件调用mysql查询,这会引发错误。
import pandas
import pandasql
fl=pandas.read_csv('file.csv')
文件的列是
fl.columns
Index([ u'time', u'contact', u'address'], dtype='object')
查询是
q=u"""
SELECT contact FROM fl LIMIT 50
"""
我以哪个身份运行
fl_solution=pandasql.sqldf(q,locals())
我得到的错误是:-
ProgrammingError: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings.
python大神给出的解决方案
尝试这样做:
fl=pandas.read_csv('file.csv',encoding='utf-8')
它指定将文件编码为utf-8
编码。此外,无论您使用从文件还是外部源获取的字符串,都应尝试将字符串编码为utf-8
。
例如:
stringname.encode('utf-8')