Python json杞琫xcel

# -*- coding: utf-8 -*-

import sys

import os

import xlwt

from xlwtimport Workbook

book= Workbook(encoding='utf-8')

reload(sys)

sys.setdefaultencoding('utf-8')

student= list()

def get_json_info():

聽 聽 uipath= unicode('E:/test_json.txt', "utf8")

with open(uipath, 'r')as f:

聽 聽 聽 聽 data= f.read().strip().decode('gbk').encode('utf-8')

_student= eval(data)

print data

# student = list()

聽 聽 聽 聽 for iin range(1, 4):

聽 聽 聽 聽 聽 聽 info= _student[str(i)]

student.append(i)

student.extend(info)

row = len(_student)/len(student)

def horz_left(x, y, data,table):

聽 聽 algnt= xlwt.Alignment()

algnt.horz= xlwt.Alignment.HORZ_LEFT

style= xlwt.XFStyle()

style.alignment= algnt

table.write(x, y, data, style)

def json_2_excel():

聽 聽 get_json_info()

table= book.add_sheet('list')

for iin range(len(student)):

聽 聽 聽 聽 if not i% 12:

聽 聽 聽 聽 聽 聽 horz_left(i//12, i% 12, student[i], table)

else:

聽 聽 聽 聽 聽 聽 table.write(i//12, i% 12, student[i])

save_path= 'E:/test_json.xls'

聽 聽 if os.path.exists(unicode(save_path, "utf8")):

聽 聽 聽 聽 os.remove(save_path)

book.save(save_path)

#{"1":["erre","jhjh","iio","state",4,65,23,56,2,45,54],"2":["info","common","file","action",67,34,1,65,687,32,54],"3":["allow","follow","test","brilstr",8,43,34,32,45,12,34]}

if __name__== '__main__':

聽 聽 json_2_excel()

你可能感兴趣的:(Python json杞琫xcel)