Python實現(xiàn)將Excel轉(zhuǎn)換為json的方法示例
本文實例講述了Python實現(xiàn)將Excel轉(zhuǎn)換為json的方法。分享給大家供大家參考,具體如下:
#-*- encoding:utf-8 -*-
import sys
import locale
import os.path
import os
import time
import shutil
import datetime
import types
import sqlite3
import pypyodbc
import traceback
import json
import codecs
import xlrd
import xlwt
from xlutils.copy import copy
# 確定運行環(huán)境的encoding
__g_codeset = sys.getdefaultencoding()
if "ascii"==__g_codeset:
__g_codeset = locale.getdefaultlocale()[1]
#
def object2double(obj):
if(obj==None or obj==""):
return 0
else:
return float(obj)
#end if
#
def utf8_to_mbs(s):
return s.decode("utf-8").encode(__g_codeset)
#
def mbs_to_utf8(s):
return s.decode(__g_codeset).encode("utf-8")
#
def _tongjiFirstRow():
#xlrd.Book.encoding = "gbk"
data = xlrd.open_workbook("xy.xls",formatting_info=True)
tblTDLYMJANQSXZB = data.sheets()[0]
#找到有幾列幾列
nrows = tblTDLYMJANQSXZB.nrows #行數(shù)
ncols = tblTDLYMJANQSXZB.ncols #列數(shù)
totalArray=[]
arr=[]
for i in range(0,ncols):
arr.append(tblTDLYMJANQSXZB.cell(0,i).value);
#end for
for rowindex in range(1,nrows):
dic={}
for colindex in range(0,ncols):
s=tblTDLYMJANQSXZB.cell(rowindex,colindex).value
dic[arr[colindex]]=s
#end for
totalArray.append(dic);
#end for
a=json.dumps(totalArray,ensure_ascii=False)
file=codecs.open("xy.txt","w",'utf-8')
file.write(a)
file.close()
#end
_tongjiFirstRow();
print("export OK")
Excel文件

json

PS:關(guān)于json操作,這里再為大家推薦幾款比較實用的json在線工具供大家參考使用:
在線JSON代碼檢驗、檢驗、美化、格式化工具:
http://tools.jb51.net/code/json
JSON在線格式化工具:
http://tools.jb51.net/code/jsonformat
在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson
json代碼在線格式化/美化/壓縮/編輯/轉(zhuǎn)換工具:
http://tools.jb51.net/code/jsoncodeformat
在線json壓縮/轉(zhuǎn)義工具:
http://tools.jb51.net/code/json_yasuo_trans
更多Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python操作Excel表格技巧總結(jié)》、《Python操作json技巧總結(jié)》、《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
Python中的數(shù)據(jù)類dataclass解讀
這篇文章主要介紹了Python中的數(shù)據(jù)類dataclass使用,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01

