0

On running python 2.7 script with code

with open(excelfile,'r') as openexcel:
    for line in openexcel:
        emp_firstname=line.split(',')[3]
        emp_lastname=line.split(',')[2]
        cursor.execute('SELECT id,Firstname,LastName FROM employee where 
        FirstName=? and LastName=?',emp_firstname,emp_lastname)

Here after emp_firstname=line.split(',')[3], emp_firstname value is Narélen which throws an error as,

cursor.execute('SELECT id,Firstname,LastName FROM employee where FirstName=? and LastName=?',emp_firstname,emp_lastname)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 3: ordinal not in range(128)

I tried some google search too but still far away from expected result. Please suggest some way to get out of this error.

Mike Scotty
  • 10,530
  • 5
  • 38
  • 50
  • take a look at https://stackoverflow.com/questions/21129020/how-to-fix-unicodedecodeerror-ascii-codec-cant-decode-byte – jeffffc Nov 30 '17 at 10:01
  • On adding, import sys reload(sys) sys.setdefaultencoding('utf8') It threw no error but ended up without doing anything. – Ashish Gupta Nov 30 '17 at 10:09
  • Please post the __full__ traceback, and the type of `emp_firstname` and `emp_lastname` (adds a `print(type(emp_firstname)); print(type(emp_lastname))` in your code and check your terminal). – bruno desthuilliers Nov 30 '17 at 10:36
  • I have got my answer by trying cursor.execute('SELECT id,Firstname,LastName FROM employee where FirstName=? and LastName=?',emp_firstname.decode('cp1252'),emp_lastname.decode('cp1252')) Thanks all for your help!! – Ashish Gupta Nov 30 '17 at 12:10

0 Answers0