Python converts an Excel file to a json file.

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.