Forums

sitecustomize.py is not working

Hi, i am new to python, i have developed one xls writing programme, while am writing to xls file am getting error with unicodedecodeerror:'ascii' codec can't decode byte 0xa8 in position 77: ordinal not in range(128), so i have try to change decoding code to 'utf_8' but still its showing the same error my code to change encoder is given below

 import sitecustomize
 import sys
 reload(sys)
 sys.setdefaultencoding('utf_8')

Need help please. Thank you.

Hi there, in my experience, sys.setdefaultencoding doesn't really work. The best thing to do is specify an encoding explicitly whenever you want to read or write to a file, with, eg

my_string.encode('utf8') # for writing
some_bytes.decode('utf8')  # for reading

thanks for your reply, actually am using xls write function. So if you don't mind can get how to use it with xls function. Am new to python, my xls write code given below. Thank you

discrptn = xxxxxx
rb = xlrd.open_workbook("D:/test.xls",formatting_info = True)
r_sheet = rb.sheet_by_index(0)
r =r_sheet.nrows
wb = copy(rb)
sheet = wb.get_sheet(0)
sheet.write(r,3,discrptn)
wb.save('D:/test.xls')

Then my guess is that your discrptn variable contains a character that can't be encoded for xls files. Check the docs for xlrd for how to deal with character encoding.

I think so, i have found that one character is making issue, my discrptn is read from perforce, so i can write some description to excel, but one line only make issue. i have noted that ' � ' this character make issue. can you suggest any solution for this ? Thank you

I have try the same with python version 2.7 and 3.4. but error showing in both version.

so- there is a particular character/line that's causing python to not be able to decode it in utf8? is it perhaps not in utf8?

Thanks for your support . . . Its working fine now

Great! What turned out to be the problem?

i have updated version to 3.4