user4069366
user4069366

Reputation:

mysql pandas unicode error while querying on columns

I have a csv file which Im reading via pandas.Then Im trying to call a mysql query on that file which is throwing up error.

import pandas
import pandasql
fl=pandas.read_csv('file.csv')

Columns of the file are

fl.columns
Index([ u'time', u'contact', u'address'], dtype='object')

The query is

q=u"""
SELECT contact FROM fl LIMIT 50
"""

WHich im running as

fl_solution=pandasql.sqldf(q,locals())

The error Im getting is :-

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.

Upvotes: 2

Views: 778

Answers (2)

user4069366
user4069366

Reputation:

Tried using a different encoding as per suggestion by harshad.

encoding = "ISO-8859-1" , and thereafter the mysql queries are working fine too.

Upvotes: 2

harshad
harshad

Reputation: 440

Try doing this:

fl=pandas.read_csv('file.csv',encoding='utf-8')

It specifies to encode file into utf-8 encoding. Also try to encode the strings into utf-8 wherever you are using strings fetched from either file or external source.

For e.g.:

stringname.encode('utf-8')

Upvotes: 3

Related Questions