Python extracts specified data from a JSON file and saves it in a CSV or Excel table file

This article introduces a method   based on the Python language to read data in JSON format, extract the specified content, and save the extracted data into .csva format or table file..xlsx

  JSON format data is often used in the process of data information exchange, but it is relatively unintuitive; therefore, sometimes we want to convert JSON format data into Excel table file data; here we will introduce how to convert JSON data based on Python language Methods for formatting and formatting data..csv.xlsx

  First, let’s look at our needs. Based on the Postman software, we have now obtained a large amount of data recorded in JSON1 format from a certain website. Some of the data is shown in the figure below (here is a sample of a large number of data samples). Here is the method for Postman to obtain website data. If you need it, you can refer to the article Basic usage of Postman software: The browser copies the request information and imports it into the software to test and send the request (https://blog.csdn.net/zhebushibiaoshifu/article /details/132383361).

  What we want to achieve now is to extract the text part (that is, the valuable information part) in the above JSON data and save it in an Excel table file; where different columns are different information attributes , and different rows Just different samples .

  Once the requirements are clear, we can start writing code. It should be noted here that the Python library needs to be used in the code of this article json. For the configuration of this library, you can refer to the article Mac system Anaconda environment configuration Python json library (https://blog.csdn.net/zhebushibiaoshifu/article/ details/132565661).

  First, the code for converting JSON format data into .csvfile data is introduced, as follows.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 29 10:22:23 2023

@author: fkxxgis
"""

import json
import csv

with open('/Users/didi/Documents/response.json', 'r') as f:
    data = json.load(f)

with open('/Users/didi/Documents/Data_All.csv', 'w', newline='', encoding='utf-8') as csvfile:
    csvwriter = csv.writer(csvfile)
    
    header = ["xkzh", "qymc", "gmpZsh", "cym", "shren", "shrq"]
    csvwriter.writerow(header)
    
    for row in data['rows']:
        xkzh = row['xkzh']
        qymc = row['qymc']
        gmpZsh = row['gmpZsh']
        cym = row['cym']
        shren = row['shren']
        shrq = row['shrq']
        
        csvwriter.writerow([xkzh, qymc, gmpZsh, cym, shren, shrq])

  First of all, it needs to be explained that after the above code is executed, when I open the newly created .csvformat file, the Chinese characters will be garbled, as shown in the figure below.

  However, using the code exported to a format file in the following article .xlsxwill not have this problem, so I did not further study the reasons for the appearance of garbled characters and directly used the subsequent code. If you are interested, you can further study the above code.

  The specific meaning of the above code is as follows. First, we with open('/Users/didi/Documents/response.json', 'r') as f:open response.jsonthe file named (that is, the file that stores our JSON format data) and assign it to a variable f; here it 'r'means opening the file in read-only mode. The code then data = json.load(f)uses json.load()a function to load the data from the JSON file and stores it in a variable data.

  Next, open Data_All.csvthe file named and assign it to a variable csvfile. 'w'Indicates that the file is opened in write mode. newline=''and used to set newlines and encoding when encoding='utf-8'writing files. .csvSubsequently, csvwriter = csv.writer(csvfile)it means creating a .csvwriter to write data to csvfilethe file.

  Secondly, we can define .csvthe header (column name) of the file and store it in headera variable in the form of a list; then, csvwriter.writerow(header)write the header to .csvthe file.

  Immediately afterwards, for dataeach row of data in, perform the following operations - xkzh = row['xkzh']it means extracting the xkzhvalue of key from the dictionary of the current row and assigning it to the variable xkzh; the following other lines also have the same meaning. Finally, we write the extracted data to .csva line in the file in the form of a list.

  Next, we introduce the code to convert JSON format data into .xlsxfile data, as follows.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 29 10:42:26 2023

@author: fkxxgis
"""

import json
from openpyxl import Workbook

with open('/Users/didi/Documents/Veterinary/response_2.json', 'r') as f:
    data = json.load(f)

wb = Workbook()
ws = wb.active

header = ["qymc", "tym", "gg", "spm", "pzwh", "zxbz", "pzrq", "yxq", "sxyy", "bgqk"]
ws.append(header)

for row in data['rows']:
    qymc = row['qymc']
    tym = row['tym']
    gg = row['gg']
    spm = row['spm']
    pzwh = row['pzwh']
    zxbz = row['zxbz']
    pzrq = row['pzrq']
    yxq = row['yxq']
    sxyy = row['sxyy']
    bgqk = row['bgqk']
    
    ws.append([qymc, tym, gg, spm, pzwh, zxbz, pzrq, yxq, sxyy, bgqk])

wb.save('/Users/didi/Documents/Veterinary/Result_2.xlsx')

  The meaning of the above code is also relatively simple.

  First, we open response_2.jsonthe file named and assign it to a variable f. 'r'Indicates that the file is opened in read-only mode. The subsequent data = json.load(f)representation uses json.load()a function to load the data from the JSONdata file and stores it in a variable .

  Next, create a new Excel workbook and assign it to the variable wb; then, get the active worksheet of the workbook and assign it to the variable ws.

  Next, we define the header (column name) of the Excelheader file, store it in a variable in list form, and write the header into the first line of the Excel file. Then, for dataeach row of data in (assuming each row is a dictionary), perform the following operations - extract the values ​​of specific fields from the dictionary of the current row and assign them to the corresponding variables. Next, we write the extracted data into a row of the Excel file in the form of a list.

  Finally, you can save the Excel workbook as Result_2.xlsxa file named.

  By running the above code, we can Result_2.xlsxsee the extracted data in the file, where each row is a sample, and each column represents an attribute, and there is no garbled code. As shown below.

  At this point, you're done.

Welcome to follow: Crazy Learning GIS

Guess you like

Origin blog.csdn.net/zhebushibiaoshifu/article/details/132572161