Chapter III reptile learning data storage

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:

  1. Objects (dictionary). Use curly braces.
  2. Array (list). Use square brackets.
  3. Plastic, float, boolean type null there.
  4. 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 jsonat dumpthe time, only store asciicharacters, so the Chinese will be escaped, this time we can use ensure_ascii=Falseto turn off this feature.
In the Pythonmiddle. Only basic data types can be converted to JSONstring format. That intis: float, str, list, dict, tuple, .

The json data directly dumpto a file:

jsonIn addition to the module dumpsfunction, and a dumpfunction that a file pointer can be passed directly to the string dumpto 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 writerobject that uses two main methods. One is writerowthat this is written in a row. One is writerowsthat 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 DictWriterup. 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:

  1. In the official website: https://dev.mysql.com/downloads/windows/installer/5.7.html
  2. If you prompted no .NET Frameworkframe. Then find the download link in the prompt box, a download on it.
  3. If there is no prompt Microsoft Virtual C++ x64(x86), then Baidu or Google software can be installed.
  4. If not found. So I whisper.

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, pymysqland so on. Here, we choose to use pymysql. Installation is very simple, the command pip install pymysqlto install.

Database linkage:

Before the database connection. First, make sure the work is completed, we are here to a pymysql_testdatabase The following describes connection mysqlsample 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 pymysqlquery data. You can use fetch*the method .

  1. fetchone(): This method of acquiring each piece of data.
  2. fetchall(): This method receives all return results.
  3. 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 fetchmanyto 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()

 

Guess you like

Origin www.cnblogs.com/lcy0302/p/10981383.html