Day 10 | day 12 python, file operation

Time:2021-11-29

In the process of developing the system, XML is often used to store and transmit data. XML is a structural markup language for marking electronic files, which is often seen in blogs.

JSON is a lightweight data exchange format, which is often used in back-end and front-end data interaction. For example, if you use Python to provide back-end WebService data interface, the data received by app when calling this interface is JSON file.

Txt is the most common file format. It mainly stores text information, that is, text information. Now most operating systems use Notepad and other programs to save, and most software can view, such as Notepad, browser and so on.

10.1 XML file

XML refers to extensible markup language (XML)Xtensible Markup Language)。 It consists of paired tags such as < book > < / book > or closed-loop tags < book / >. The outermost tag is called the root node. The root node can contain many child nodes, which have their own attributes and text. The following example: book is the root node, name has its own attribute name, the text value of author is Lao Chen, and price is the closed-loop label.

<book>
< name = "book title" > finish Python in 12 days < / name >
< author > Lao Chen < / author >
< price = "0.00 yuan" / >
</book>

In the built-in standard library, python provides a large number of packages and tools that can be used to process XML language. After testing and comparing with my colleagues, I found that the elementtree module is the best. I recommend you also use it to process XML files.

1. Create XML

XML file, starting from the creation of the node (Book), and then create the child nodes inside, and add the attributes and contents of the child nodes.

import xml.etree.ElementTree as ET

#Create root node
book = ET.Element("book")
#Create child nodes and add attributes and data
title = ET.SubElement(book, "name")
Title. Attrib = {"name": "title"}
Title.text = "12 days"
#Create child nodes and add data
author = ET.SubElement(book, "author")
Author.text = "Lao Chen"
price = ET.SubElement(book, "price")
#Create child nodes and add data
Price. Attrib = {"price": "0.00 yuan"}
#Create a tree object and write a file
tree = ET.ElementTree(book)
tree.write("book.xml", encoding="UTF-8")

Output results (book. XML file)

<book>
< name = "book title" > finish Python in 12 days < / name >
< author > Lao Chen < / author >
< price = "0.00 yuan" / >
</book>

2. Modify XML

Modify the book.xml file, add the creation date, modify the attribute name of name to arr, and adjust the price to 0.01 yuan.

import xml.etree.ElementTree as ET


Tree = et.parse ("book. XML") # read the file to be modified
book = tree.getroot()
#Create a new node and add it as a child of root
createDate = ET.Element("createDate")
Createdate.attrib = {"name": "creation date"}
createDate.text = "2020-10-01"
book.append(createDate)
#Modify the properties of the book title
name = book.find("name")
name.attrib = {"name": "arr"}
#Modify price
price = book.find("price")
Price.text = "0.01 yuan"
#Write back to the original file
tree.write("book.xml", encoding="UTF-8")

Output results (book. XML file)

<book>
< name = "arr" > Python in 12 days < / name >
< author > Lao Chen < / author >
< price price = "0.00 yuan" > 0.01 yuan < / price >
< createDate name = "creation date" > 2020-10-01 < / createDate >
</book>

3. Delete XML node

Find the deletion point first, and then delete it with the remove () method.

import xml.etree.ElementTree as ET

Tree = et.parse ("book. XML") # read the file to be modified
book = tree.getroot()
for price in book.findall('price'):
    book.remove(price)

tree.write('book.xml', encoding="UTF-8")

Output results (book. XML file)

<book>
< name = "arr" > Python in 12 days < / name >
< author > Lao Chen < / author >
< createDate name = "creation date" > 2020-10-01 < / createDate >
</book>
  1. Read XML

Read the node content circularly and output the node that is not none.

import xml.etree.ElementTree as ET

Tree = et.parse ("book. XML") # read the file to be modified
book = tree.getroot()
for node in book.iter():
    if node.text is not None:
        print(node.tag, ":", node.text)

Well, old fellow Chen has finished talking about the contents of the XML file. If you feel that it is helpful to you, I hope that Lao tie can send some praise to it, so that more people can see this article. Your forwarding and praise are the greatest encouragement for Lao Chen to continue to create and share.

10.2 JSON file

JSON file data consists of a series of key value pairs separated by commas, which can be transformed with the dictionary we have learned. In Python, JSON module can be used to encode and decode JSON data.

1. Create JSON file

json.dumps()Encoding string data,json.dump()Encode the file data.

import json

#Convert dictionary to JSON object
book = {
    'name': "finish Python in 12 days",
    'author': 'Lao Chen',
    'price': 0.00
}

#Generate JSON format
json_str = json.dumps(book, ensure_ascii=False)
#Write JSON data
with open('book.json', 'w') as f:
    json.dump(json_str, f)

Output results (contents of book.json file)

"{" name ":" Python in 12 days "," author ":" Lao Chen "," price ": 0.0}"

2. Read JSON file

json.loads()Decoding string data,json.load()Decode the JSON file.
……

I can’t copy any more. I’ll finish Python in 12 days and finish it tomorrow. Next, I’ll release Python data analysis and crawler in 16 days. If you need friends, you can go and have a look.

Recommended Today

On the mutation mechanism of Clickhouse (with source code analysis)

Recently studied a bit of CH code.I found an interesting word, mutation.The word Google has the meaning of mutation, but more relevant articles translate this as “revision”. The previous article analyzed background_ pool_ Size parameter.This parameter is related to the background asynchronous worker pool merge.The asynchronous merge and mutation work in Clickhouse kernel is completed […]