要求:
在做一些特定的格式化之后,我必须将一个pandas数据帧写入excel。当我使用3.4在OS X中运行时,一切都很棒,但随后将代码移动到标准化的2.7.5版本中。而现在它正在打破奇怪的错误。非常感谢任何解决方案。 代码段:
#WRITING DIFF OUTPUT
if(len(group_of_2_df)!= 0):
writer = xlsxwriter.Workbook("SNAPSHOT_"+ str(datetime.datetime.today().strftime("%Y%m%d%H%S"))+ ".xlsx") # Parameterize
xWorksheet = writer.add_worksheet("sheet_1")
format1 = writer.add_format({'bold': False, 'font_color' : 'blue'})
format2 = writer.add_format({'bold': False, 'font_color' : 'black'})
format3 = writer.add_format({'bold': False, 'font_color' : 'black'})
format4 = writer.add_format({'bold': False, 'font_color' : 'black'})
#### Add Header to Excel
for idx,column in enumerate(df_header):
xWorksheet.write(0,idx,df_header[idx])
# rows --> dataframe range
#Putting Last column to first
original_cols = group_of_2_df_update.columns.tolist()
original_cols = original_cols[-1:] + original_cols[:-1]
group_of_2_df_update = group_of_2_df_update[original_cols]
for rows in range(len(group_of_2_df_update)):
# columns ---> df_header length
for cols in range(len(df_header)):
# added utf-8 encode lets see what happens
val = str(group_of_2_df_update.iloc[rows,cols].encode('utf8'))
#print("value is",val)
if(val == 'Insert'):
format1.set_bg_color("pink")
format1.set_font_color("black")
#print("I am in >> loop")
xWorksheet.write(rows+1,cols,val,format1)
for sml_col in range(1,len(df_header)):
#print("what is my row_col", sml_col)
xWorksheet.write(rows+1,sml_col, str(group_of_2_df_update.iloc[rows,sml_col].encode('utf8')),format1)
break #Break the row if pure insert
elif (val[0:2] != '<>' ):
format3.set_bg_color('white')
format3.set_font_color('black')
xWorksheet.write(rows+1,cols,val,format3)
elif(val[0:2] == '<>'):
#print("I am in <> loop")
# print("inside if value now", val)
format2.set_bg_color('yellow')
format2.set_font_color( 'green')
xWorksheet.write(rows+1,cols,val[2:],format2)
elif(val[0:3] == 'Row'):
format4.set_bg_color('red')
format4.set_font_color('black')
xWorksheet.write(rows+1,cols,val,format4)
if(len(group_of_2_df)!= 0):
writer.close()
我尝试将其编码为utf-8,因为我尝试编写的数据将具有日语/欧洲字符。 现在它失败并出现以下错误:
Traceback (most recent call last):
File "/export/home/s***/Projects/ema_avails_dummy/bin/ema_avails_version_8_20160927.py", line 457, in <module>
writer.close()
File "/export/home/s***/.venv/lib/python2.7/site-packages/xlsxwriter/workbook.py", line 297, in close
self._store_workbook()
File "/export/home/s****/.venv/lib/python2.7/site-packages/xlsxwriter/workbook.py", line 605, in _store_workbook
xml_files = packager._create_package()
File "/export/home/s****/.venv/lib/python2.7/site-packages/xlsxwriter/packager.py", line 139, in _create_package
self._write_shared_strings_file()
File "/export/home/s****/.venv/lib/python2.7/site-packages/xlsxwriter/packager.py", line 286, in _write_shared_strings_file
sst._assemble_xml_file()
File "/export/home/s****/.venv/lib/python2.7/site-packages/xlsxwriter/sharedstrings.py", line 53, in _assemble_xml_file
self._write_sst_strings()
File "/export/home/s****/.venv/lib/python2.7/site-packages/xlsxwriter/sharedstrings.py", line 83, in _write_sst_strings
self._write_si(string)
File "/export/home/s****/.venv/lib/python2.7/site-packages/xlsxwriter/sharedstrings.py", line 110, in _write_si
self._xml_si_element(string, attributes)
File "/export/home/s****/.venv/lib/python2.7/site-packages/xlsxwriter/xmlwriter.py", line 122, in _xml_si_element
self.fh.write("""<si><t%s>%s</t></si>""" % (attr, string))
File "/export/home/s****/.venv/lib/python2.7/codecs.py", line 691, in write
return self.writer.write(data)
File "/export/home/s****/.venv/lib/python2.7/codecs.py", line 351, in write
data, consumed = self.encode(object, self.errors)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 16: ordinal not in range(128)
我将安装并尝试使用django,但如果在任何情况下都可以避免其他导入,那么该解决方案将受到高度赞赏。