I am trying to write a method so that the data can be exported from DB to Excel file. I am using the XlsxWriter library for this.
This is the main function
def user_stats_to_excel (): workbook = xlsxwriter.Workbook ('user_stats_test.xlsx') worksheet = Workbook.add_worksheet () workheet.write_row (0) , 0, UserStats.OUTPUT_ORDER) for i, user_stat enumerate (UserStats.objects.all ()): worksheet.write_row (i + 1, 0, user_stat.output ()) for Workbook.close ()
And then in my model I have:
def production (self, data = none): "" "back to list" for some CSV writing If the data is ready: self.data = data dir_self = dir (self) lst = [] for the name in itself. O: If 'do_' + names in dir_self: out = getattr Yes, "Do_" + name (name) if not outside: out = u "" if ians (outside, list): lst.extend (map (self.safe_encode, outside)) Other: try: lst.append Except the exception (out.encode ('utf-8')): lst.append (outside) Other: Try: lst.append (self.data.get (name, "") .encode ('utf-8' ) Except exception: lst.append (self.data.get (name, "")) returns LST def secure_ADD (self, data): try: return data except exception. Exode ('UTF-8'): Return data As you can see, Has put Etik words because it is general improvement with Unicode problems in Python.
My Stacktrace is:
Tracebacks (Last Call Last): File "& lt; Console>", Line 1, & lt; Module & gt; File "/Users/james/pubfront/riidr/apps/stats/admin.py", line 34, in the user_stats_to_excel workbook.close () file "/Users/james/pubfront/riidr_venv/lib/python2.7/site-packages /xlsxwriter/workbook.py ", line 286, close the self ._store_workbook () file" / user / jams / pubfruit / liid / lib / python2.7 / site-packages / xlsxwriter / workbook.py ", line 509, in _store_workbook xml_files = packager._create_package (), file "/Users/james/pubfront/riidr_venv/lib/python2.7/site-packages/xlsxwriter/packager.py", line (140 _create_package self._write_shared_strings_file) file "/ user /james/pubfront/riidr_venv/lib/python2.7/site-packages/xlsxwriter/packager.py ", line 280, in _write_shared_strings_file sst._assemble_xml_file () file" / users / james / pubfront / riidr_venv / l ib / python2.7 /site-packages/xlsxwriter/sharedstrings.py ", line 53, _assemble_xml_file (in self._write_sst_strings) file" /Users/james/pubfront/riidr_venv/lib/python2.7/site-packages/xlsxwriter/ sharedstrings.py ", line 83, self._w rite_si (string _write_sst_strings) file" /Users/james/pubfront/riidr_venv/lib/python2.7/site-packages/xlsxwriter/sharedstrings.py ", line 110, _write_si self In the ._xml_si_element file ("string", "/ users / james / pubfront / riidr_venv / lib / python2.7 / site-package / xlsxwriter / xmlwriter.py", line 122, _xml_si_element in self.fh.write ("" " & Lt; C & gt; & Lt; T% s>% s & LT; / T & gt; & Lt; / Si> "" "% (Attr, string)) file" / user / James / Pbfrnt / Araidiar_wvel / Elaibi / Paithon 2.7 / Kodksap ", line 688, self-written returns. Write (data) file" / Users / Jmejhe / Pbfrnt / Araidiar_wvel / lib / Paithon 2.7 / Kodkaps ", line 351, in writing data, consumption = self.encode (object, self. errors) Unikoddekod error: 'Assiai' codec situation can not decode byte 0xc3 8: serial number No (128)
me This is a real problem to debug because the workbook.) Is called in user_stats_to_excel, so it would be difficult to find a user triggering this problem. I am hoping that I am not the only person for this problem
We have started using the Dzena utilities to handle the encoding:
If you are not using the daisies then The kitchen library has the same functions:
The only thing we had to do was to exclassify the URL to behave in the form of a wire
workbook = xlsxwriter Workbook (filename, {'strings_to_urls': wrong})
No comments:
Post a Comment