开发者

xlwt data garbled

开发者 https://www.devze.com 2022-12-28 06:40 出处:网络
I retrieve the data of chinese characters from DB and write the data into excel by xlwt, code as below:

I retrieve the data of chinese characters from DB and write the data into excel by xlwt,

code as below:

ws0.write(0,0, unicode(cell, '开发者_高级运维big5'))

It is ok under Windows, but when I deloyed it under Linux, the data in excel garbled,

Could you help to do with it?


It would help if you posted the code that you actually ran. Assuming that ws0 is a Worksheet object, the correct syntax is ws0.write(row_index, column_index, unicode_text).

What does cell refer to, and how did you extract it from what database?

What does "the data in excel garbled" mean? What are you using on Linux to view the contents of the XLS file? What did you actually see on the screen? Can you get Chinese characters displayed properly on Linux with other software?

Try typing this at the Python interactive prompt on Linux:

>>> import xlwt
>>> b = xlwt.Workbook()
>>> s = b.add_sheet('zh')
>>> big5_text = '\xa7A\xa6n\xa1I'
>>> u_text = big5_text.decode('big5')
>>> s.write(0, 0, u_text)
>>> b.save('nihao.xls')

Then try opening the XLS file with OpenOffice Calc ... what do you see?

Update

(1) "The code that you ran" must have been more than 1 line; please show it.

(2) Please run the small piece of code I gave you, and report the results. If that works, we can concentrate on things like how you are getting what data out of what database [Please answer that question too]

(3) Please answer the question about displaying Chinese under Linux.

(4) Consider that seeing "???" instead of Chinese (or whatever) characters is usually the result of unicode_text.encode('some_encoding', 'replace') (or other code with the same intent) with an inappropriate encoding (for example, 'ascii') -- perhaps preceded by a similar decode. xlwt does unicode_text.encode() to store your unicode strings in the file; it uses 'latin1' or 'utf_16le' as required for the encoding, and 'strict', not 'replace', for the next arg. If Excel is showing you "???", it is likely that the data is already garbled before you feed it to xlwt. What does print repr(cell) tell you?

(5) If you run the same versions of xlwt and Python with the same input data and the same Python script, the output file from Linux should be identical byte-for-byte with the output file from Windows. Differences in xlwt and Python versions are rather unlikely to make the files differ. Please compare the files that result from the short script that I gave you, using a binary comparison (for example, fc /b ... in a Windows "Command Prompt" window). Please state the versions of Python and xlwt that you are using in each environment.

(6) Please consider switching over to the usual forum for xlwt questions ... that way you can easily send me files to look at if necessary, and I get e-mail when a new posting is made, instead of having to poll a website at intervals ...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号