Python converts an Excel file to a json file.
This example describes how to convert an Excel file to a json file using Python. We will share this with you for your reference. The details are as follows:
#-*-Encoding: UTF-8-*-import sysimport localeimport OS. pathimport osimport timeimport shutilimport datetimeimport typesimport sqlite3import pypyodbcimport tracebackimport jsonimport codecsimport xlrdimport xlwtfrom xlutils. copy import copy # determine the runtime environment's encoding _ g_codeset = sys. getdefaultencoding () if "ascii" =__ g_codeset: _ g_codeset = locale. getdefalocallocale () [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] # Find several columns of nrows = tblTDLYMJANQSXZB. nrows # number of rows ncols = tblTDLYMJANQSXZB. ncols # Number of columns totalArray = [] arr = [] for I in range (0, ncols): arr. append (tblTDLYMJANQSXZB. cell (0, I ). value); # end 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 files
Json
PS: For json operations, we recommend several useful json online tools for your reference:
Online JSON code verification, validation, beautification, and formatting tools:
Http://tools.jb51.net/code/json
JSONOnline formatting tool:
Http://tools.jb51.net/code/jsonformat
Online XML/JSON conversion tools:
Http://tools.jb51.net/code/xmljson
JsonCode Online formatting/beautification/compression/editing/conversion tools:
Http://tools.jb51.net/code/jsoncodeformat
Online json compression/escape tools:
Http://tools.jb51.net/code/json_yasuo_trans
For more Python-related content, refer to this topic: python Excel table skill summary, Python json skill summary, Python coding skill summary, Python data structure and algorithm tutorial, and Python function usage skill Summary "," Python string operation skills summary "," Python entry and advanced classic tutorial ", and" Python file and directory operation skills summary"
I hope this article will help you with Python programming.