tags:

views:

138

answers:

1

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?

A: 

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 *de*code. 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 ...

John Machin
Actually I mail the excel as attached file genernated under linux and view the attached file under windows, it is shown as ??? ,while the file genernated under windows is ok.
e.b.white
Now type in that small script on Linux and mail the result to Windows. What do you see?
John Machin
I have run your script and it is ok.I think it is some problem about cx_Oracle module.(I use cx_Oracle to get data from oracle)On window the cx_Oracle is cx_oracle_4.4.1.9i(python 2.5.2, python 2.5 xlwt-0.72 ,with full 10g oracle installation), and on Linux is cx_oracle_5.0.3-11g(python 2.4.3, oracle-instantclient11.2-basic-11.2.0.1.0-1.i386)So I try to install cx_Oracle for 9i or 10g with instantclient, but the errors reported: undefined sysmbol:OCIDB Shutdown
e.b.white
Do you think it possible to do some **simple** debugging before doing drastic things like trying to install a new version of cx_Oracle? Why do you "think it is some problem about cx_Oracle"? Have you examined what you are getting out of the database? Is it valid big5 text?
John Machin
I have solved the problem after setting NLS_LANG environment variables.Anyway, thanks very much for your suggestion.
e.b.white