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)文章
pandas 實現(xiàn)某一列分組,其他列合并成list
這篇文章主要介紹了pandas 實現(xiàn)某一列分組,其他列合并成list的案例。具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03python 利用已有Ner模型進行數(shù)據(jù)清洗合并代碼
今天小編就為大家分享一篇python 利用已有Ner模型進行數(shù)據(jù)清洗合并代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12淺談opencv自動光學檢測、目標分割和檢測(連通區(qū)域和findContours)
這篇文章主要介紹了淺談opencv自動光學檢測、目標分割和檢測(連通區(qū)域和findContours),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06基于Python+Pyqt5開發(fā)一個應(yīng)用程序
今天給大家?guī)淼氖顷P(guān)于Python的相關(guān)知識,文章圍繞著Python+Pyqt5開發(fā)一個應(yīng)用程序展開,文中有非常詳細的介紹及代碼示例,需要的朋友可以參考下2021-06-06Python一行代碼實現(xiàn)ChatGPT接入微信機器人
這篇文章主要為大家介紹了Python一行代碼實現(xiàn)ChatGPT接入微信機器人示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03使用pycharm連接讀取orcl數(shù)據(jù)庫的表的操作方法
這篇文章主要介紹了使用pycharm連接讀取orcl數(shù)據(jù)庫的表的操作方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-01-01python基于OpenCV模塊實現(xiàn)視頻流數(shù)據(jù)切割為圖像幀數(shù)據(jù)(流程分析)
這篇文章主要介紹了python基于OpenCV模塊實現(xiàn)視頻流數(shù)據(jù)切割為圖像幀數(shù)據(jù),這里今天主要是實踐一下視頻流數(shù)據(jù)的預(yù)處理工作,需要的朋友可以參考下2022-05-05