This article mainly introduces the Python implementation to read the JSON file to Excel table, with a certain reference value, interested in small partners can refer to
The example of this article for everyone to share the Python implementation to read the JSON file to Excel table for your reference, the specific content is as follows
First, the demand
1, ' Score.json ' file contents:
{ "1": ["Floret", 99,100,98.5], "2": ["Xiao Wang", 90,30.5,95], "3": ["Xiao Ming", 67.5,49.6,88]}
2, read the JSON file to save to the database, and calculate the total score and the average of each person
Second, the implementation of the Code
Import JSON, Xlwtdef Read_score (jsonfile): With open (jsonfile, encoding= ' Utf-8 ') as F: # Convert the JSON file to a dictionary score_all = Json.load (f) Book = XLWT. Workbook () # Create Excel file sheet = book.add_sheet (' Sheet1 ') # Create a table title = [' Serial number ', ' name ', ' language ', ' math ', ' English ', ' total score ', ' average ' ] for col in range (len title): # Deposit First Row header sheet.write (0, col, Title[col]) Row = 1 # define row for K in Score_all : data = score_all[k] # data holds names and fractions of list data.append (sum (data[1:4)) # The penultimate column adds total data.append (sum (data[ 1:4])/3.0) # The last column adds an average of Data.insert (0, K) # The first column is added to the ordinal for index in range (len data): # Write each line Sheet.write (r OW, index, Data[index]) row + = 1 book.save (' Score.xls ') read_score (' Score.json ')