Python實(shí)現(xiàn)將Excel轉(zhuǎn)換為json的方法示例
本文實(shí)例講述了Python實(shí)現(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
# 確定運(yùn)行環(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操作,這里再為大家推薦幾款比較實(shí)用的json在線工具供大家參考使用:
在線JSON代碼檢驗(yàn)、檢驗(yàn)、美化、格式化工具:
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入門(mén)與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
基于Python實(shí)現(xiàn)本地音樂(lè)播放器的制作
這篇文章主要介紹了如何利用Python實(shí)現(xiàn)本地音樂(lè)播放器的制作,并且可以選擇需要播放的音樂(lè)的路徑,選擇播放方式,感興趣的小伙伴可以了解一下2022-06-06
Python實(shí)現(xiàn)ATM簡(jiǎn)單功能的示例詳解
這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)ATM的簡(jiǎn)單功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-11-11
python安裝TA-Lib庫(kù)報(bào)錯(cuò)問(wèn)題的解決方法
TaLib是一個(gè)Python金融指數(shù)處理庫(kù),包含了很多技術(shù)分析里的常用參數(shù)指標(biāo),例如MA、SMA、WMA、MACD、ATR等,這篇文章主要給大家介紹了關(guān)于python安裝TA-Lib庫(kù)報(bào)錯(cuò)問(wèn)題的解決方法,需要的朋友可以參考下2024-01-01
Pycharm配置autopep8實(shí)現(xiàn)流程解析
這篇文章主要介紹了Pycharm配置autopep8實(shí)現(xiàn)流程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
Python中的數(shù)據(jù)類dataclass解讀
這篇文章主要介紹了Python中的數(shù)據(jù)類dataclass使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
解決PyCharm import torch包失敗的問(wèn)題
今天小編就為大家分享一篇解決PyCharm import torch包失敗的問(wèn)題。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10

