1

I am writing an SQL query in python pandas:

import pandas as pd
from pandas import DataFrame, read_csv
import numpy as np
from pandasql import sqldf
pysqldf=lambda q:sqldf(q,globals())
rolup = pysqldf(u"select MasterUserId,DeviceUsed,hcluster, count(MasterUserId) as Tot_Rec, sum(Visits),sum(PV),sum(TimeSpent) from clstrd_data group by MasterUserId,DeviceUsed,hcluster;")

Error:

sqlite3.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.

How to switch to Unicode strings? I am using python2.7.

2
  • new code: import pandas as pd from pandas import DataFrame, read_csv import numpy as np from pandasql import sqldf pysqldf=lambda q:sqldf(q,globals()) rolup = pysqldf(u"select MasterUserId,DeviceUsed,hcluster, count(MasterUserId) as Tot_Rec, sum(Visits),sum(PV),sum(TimeSpent) from clstrd_data group by MasterUserId,DeviceUsed,hcluster;") Commented Nov 19, 2014 at 9:38
  • your "new code" is identical to what's in the question - so maybe delete your confusing comment here? Commented Jul 22, 2018 at 13:50

1 Answer 1

2

According to the python unicode howto:

In Python source code, Unicode literals are written as strings prefixed with the ‘u’ or ‘U’ character: u'abcdefghijk'

In other words, your script should read:

import pandas as pd
from pandas import DataFrame, read_csv
import numpy as np
from pandasql import sqldf
pysqldf=lambda q:sqldf(q,globals())
rolup = pysqldf(u"select MasterUserId,DeviceUsed,hcluster, count(MasterUserId) as Tot_Rec, sum(Visits),sum(PV),sum(TimeSpent) from clstrd_data group by MasterUserId,DeviceUsed,hcluster;")

Hope that helps.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @hd1. I have changed my code but still getting exactly same error.
Sir I have just pasted you code. Because only differece i could see was addition of u before select statement. Here is the new code: import pandas as pd from pandas import DataFrame, read_csv import numpy as np from pandasql import sqldf pysqldf=lambda q:sqldf(q,globals()) rolup = pysqldf(u"select MasterUserId,DeviceUsed,hcluster, count(MasterUserId) as Tot_Rec, sum(Visits),sum(PV),sum(TimeSpent) from clstrd_data group by MasterUserId,DeviceUsed,hcluster;")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.