欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)將Excel轉(zhuǎn)換為json的方法示例

 更新時(shí)間:2017年08月05日 11:34:35   作者:liumengcheng  
這篇文章主要介紹了Python實(shí)現(xiàn)將Excel轉(zhuǎn)換為json的方法,涉及Python文件讀寫及格式轉(zhuǎn)換相關(guān)操作技巧,需要的朋友可以參考下

本文實(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入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論