Chapter III data storage
Section json file processing:
What is json:
JSON (JavaScript Object Notation, JS object tag) is a lightweight data-interchange format. It is based on a subset of ECMAScript (w3c js established specification), a fully independent programming language format to store text and presentation data. Simple and clear hierarchy make JSON an ideal data-interchange language. Easy to read and write, but also easy for machines to parse and generate, and effectively improve the efficiency of network transmission. For more explanation, see: https://baike.baidu.com/item/JSON/2462549?fr=aladdin
JSON supports data formats:
- Objects (dictionary). Use curly braces.
- Array (list). Use square brackets.
- Plastic, float, boolean type null there.
- String type (string must use double quotes, not single quotes).
Among the plurality of data separated by commas.
Note: The json is essentially a string.
Dictionaries and lists turn JSON:
Import JSON Books = [ { ' title ' : ' How is the Steel excel ' , ' . price ' : 9.8 }, { ' title ' : ' Dream of the Red ' , ' . price ' : 9.9 } ] json_str = json.dumps (Books, = ensure_ascii False) Print (json_str)
Because json
at dump
the time, only store ascii
characters, so the Chinese will be escaped, this time we can use ensure_ascii=False
to turn off this feature.
In the Python
middle. Only basic data types can be converted to JSON
string format. That int
is: float
, str
, list
, dict
, tuple
, .
The json data directly dump
to a file:
json
In addition to the module dumps
function, and a dump
function that a file pointer can be passed directly to the string dump
to a file. Sample code is as follows:
= Books [ { ' title ' : ' How is the Steel excel ' , ' . price ' : 9.8 }, { ' title ' : ' Dream of the Red ' , ' . price ' : 9.9 } ] with Open ( ' a.json ' , ' W ' ) AS FP: The json.dump (Books, FP)
Json string into a load Python object:
= json_str ' [{ "title": "How is the Steel excel", "price": 9.8} , { "title": " Dream of the Red", ". price": 9.9}] ' Books = json.loads (json_str, = encoding ' UTF-. 8 ' ) Print (type (Books)) Print (Books)
Json read directly from the file:
import json with open('a.json','r',encoding='utf-8') as fp: json_str = json.load(fp) print(json_str)
Section csv file processing
Csv file reads:
import csv with open('stock.csv','r') as fp: reader = csv.reader(fp) titles = next(reader) for x in reader: print(x)
This operation, when data acquired later, it is necessary to get the data in the following table. If you want to get through the title when acquiring data. You can use DictReader
. Sample code is as follows:
import csv with open('stock.csv','r') as fp: reader = csv.DictReader(fp) for x in reader: print(x['turnoverVol'])
Write data to a csv file:
Write data to a csv file, you need to create an writer
object that uses two main methods. One is writerow
that this is written in a row. One is writerows
that this is a write multiple lines. Sample code is as follows:
import csv headers = ['name','age','classroom'] values = [ ('zhiliao',18,'111'), ('wena',20,'222'), ('bbc',21,'111') ] with open('test.csv','in',newline='') as fp: writer = csv.writer(fp) writer.writerow(headers) writer.writerows(values)
You can also use the data is written into the dictionary approach. This time you need to use DictWriter
up. Sample code is as follows:
import csv headers = ['name','age','classroom'] values = [ {"name":'wenn',"age":20,"classroom":'222'}, {"name":'abc',"age":30,"classroom":'333'} ] with open('test.csv','w',newline='') as fp: writer = csv.DictWriter(fp,headers) writer = csv.writeheader() writer.writerow({'name':'zhiliao',"age":18,"classroom":'111'}) writer.writerows(values)
Section MySQL database operations
Install mysql:
- In the official website: https://dev.mysql.com/downloads/windows/installer/5.7.html
- If you prompted no
.NET Framework
frame. Then find the download link in the prompt box, a download on it. - If there is no prompt
Microsoft Virtual C++ x64(x86)
, then Baidu or Google software can be installed. - If not found. So I whisper.
navicat:
navicat is a very convenient operation mysql database software. Use him to operate the database, just use excel operational data is the same.
Install the driver:
To operate Python MySQL. There must be a middleware, or called the driver. There are a lot of drivers. For example, there are mysqldb
, mysqlclient
, pymysql
and so on. Here, we choose to use pymysql
. Installation is very simple, the command pip install pymysql
to install.
Database linkage:
Before the database connection. First, make sure the work is completed, we are here to a pymysql_test
database The following describes connection mysql
sample code:
import pymysql db = pymysql.connect( host="127.0.0.1", user='root', password='root', database='pymysql_test', port=3306 ) cursor = db.cursor() cursor.execute("select 1") data = cursor.fetchone() print(data) db.close()
Insert data:
import pymysql db = pymysql.connect( host="127.0.0.1", user='root', password='root', database='pymysql_test', port=3306 ) cursor = db.cursor() sql = """ insert into user( id,username,gender,age,password ) values(null,'abc',1,18,'111111'); """ cursor.execute(sql) db.commit() db.close()
If in the case where data can not be guaranteed, the data may be inserted following ways:
sql = """
insert into user(
id,username,gender,age,password
)
values(null,%s,%s,%s,%s);
"""
cursor.execute(sql,('spider',1,20,'222222'))
import pymysql db = pymysql.connect( host="127.0.0.1", user='root', password='root', database='pymysql_test', port=3306 ) cursor = db.cursor() sql = """ insert into user( id,username,gender,age,password ) values(null,'abc',1,18,'111111'); """ cursor.execute(sql) db.commit() db.close()
Find data:
Use pymysql
query data. You can use fetch*
the method .
fetchone()
: This method of acquiring each piece of data.fetchall()
: This method receives all return results.fetchmany(size)
: You can get the data specified number of pieces.
Sample code is as follows:
cursor = db.cursor() sql = """ select * from user """ cursor.execute(sql) while True: result = cursor.fetchone() if not result: break print(result) db.close()
Or direct use fetchall
, disposable can satisfy all the conditions are taken out of the data:
cursor = db.cursor() sql = """ select * from user """ cursor.execute(sql) results = cursor.fetchall() for result in results: print(result) db.close()
Or use fetchmany
to specify how many data acquisition:
cursor = db.cursor() sql = """ select * from user """ cursor.execute(sql) results = cursor.fetchmany(1) for result in results: print(result) db.close()
delete data:
cursor = db.cursor() sql = """ delete from user where id=1 """ cursor.execute(sql) db.commit() db.close()
update data:
conn = pymysql.connect(host='localhost',user='root',password='root',database='pymysql_demo',port=3306) cursor = conn.cursor() sql = """ update user set username='aaa' where id=1 """ cursor.execute(sql) conn.commit() conn.close()