python 导出内容到excel
文件中存在这样的json内容:
[["shine","男","23","女"]]
需要将json内容导出到excel中,代码如下:
#!/usr/local/python2.7 # encoding=utf-8 import xlsxwriter import sys import demjson reload(sys) sys.setdefaultencoding('utf8') def export_to_excel(data): # decode json字符串 data = demjson.decode(data) file_name = 'test.xlsx' # 指定文件名&添加worksheet workbook = xlsxwriter.Workbook(file_name) worksheet = workbook.add_worksheet() row = 0 col = 0 index = 0 # 设置head head = [ '姓名', '性别', '年龄', '爱好' ] for head_item in (head): worksheet.write(row, index, head_item) index += 1 row += 1 expenses = data # 写入内容 for single in (expenses): for item in (single): worksheet.write(row, col, item) col += 1 col = 0 row += 1 workbook.close() return file_name if __name__ == "__main__": try: # 读取文件中的json内容 file = open("/Users/lianghui/mycodes/test/storage/app/content-json") res = export_to_excel(file.read()) print(res) except Exception as error: print(error)