数据储存

JSON

JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式。它基于 ECMAScript (w3c制定的js规范)的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。JSON支持对象(字典)、数组(列表)、整数、浮点数、布尔类型、null类型还有字符串类型等。多个数据之间使用逗号分开。

注意:字符串内容必须使用双引号,(不能使用单引号),且json本质就是一个字符串。

我们把对象(变量)从内存中变成可存储或传输的过程称之为序列化,在Python中叫pickling;序列化之后,就可以把序列化后的内容写入磁盘,或者通过网络传输到别的机器上;反过来,把变量内容从序列化的对象重新读到内存里称之为反序列化,即unpickling

######存储json######
import json
dic = {"name":"Hermaeus",
       "age":19,
       "hometown":"MeiShan",
       "hobby":"Coding"}

j_dic = json.dumps(dic)   #===> json.dump(dic, f)
f = open("test.json","w")
f.write(j_dic)

f.close()
######加载json######
import json
######存储json######
f = open("test.json","r")
date = json.loads(f.read()) #===> json.load(f)
print(date)

输入结果为:

{'name': 'Hermaeus', 'age': 19, 'hometown': 'MeiShan', 'hobby': 'Coding'}

注意:json在打印dump的时候,只能存放ASCII的字符,因此会将其中中文等字符进行转义,这是我们可以在传递ensure_ascii=False这个参数来关闭这个特性。

CSV文件处理

逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字符也可以不是逗号),其文件以纯文本形式存储表格数据(数字和文本)。纯文本意味着该文件是一个字符序列,不含必须像二进制数字那样被解读的数据。CSV文件由任意数目的记录组成,记录间以某种换行符分隔;每条记录由字段组成,字段间的分隔符是其它字符或字符串,最常见的是逗号或制表符。

写入

方法一
这种方法,我们需要创建一个writer对象。之后,我们可以使用writerow写入一行,或者使用writerows写入多行。例如:

import csv
headers = ["name","age","hometown"]
values = [
    ("Mingle",20,"MeiShan"),
    ("ZhangYi",19,"Chengdou"),
    ("Zhaoli",16,"NanJing")
]
with open("text.csv","w",newline="") as fp:
    writer = csv.writer(fp)
    writer.writerow(headers)
    writer.writerows(values)

这样就生成了一个叫做text.csv的文件,如下:

name,age,hometown
Mingle,20,MeiShan
ZhangYi,19,Chengdou
Zhaoli,16,NanJing

方法二
我们也可以使用字典的方式把数据写入,这时我们必须借助DictWriter了,例如:

import csv

headers = ["name","age","hometown"]
values = [
    {"name":"Mingle","age":20,"hometown":"Meishan"},
    {"name": "Aaa", "age": 17, "hometown": "NanJing"},
    {"name": "Mm", "age": 21, "hometown": "ShangHai"}
]
with open("text_1.cvs","w",newline="") as fp:
    writer = csv.DictWriter(fp,headers)
    writer.writeheader() ##虽然已经传入了headers,但是在这里依然要使用该方法写入headers
    writer.writerows(values)

读取

方法一

import csv

with open("text.csv","r") as fp:
    reader = csv.reader(fp)
    tiltles = next(reader)
    print(tiltles)
    for i in reader:
        print(i)

方法二
我们通过DictReader类来实例一个对象,可以返回字典类型。

import csv

with open("text.csv","r") as fp:
    reader = csv.DictReader(fp)
    for x in reader:
        print(x)
'''
result:
OrderedDict([('name', 'Mingle'), ('age', '20'), ('hometown', 'MeiShan')])
OrderedDict([('name', 'ZhangYi'), ('age', '19'), ('hometown', 'Chengdou')])
OrderedDict([('name', 'Zhaoli'), ('age', '16'), ('hometown', 'NanJing')])
'''

MySQLpymysql

连接

import pymysql

db = pymysql.connect(
    host = "localhost",   ##地址
    user = "root",        
    password = "######",
    db = "mysql",
    port = 3306           ##端口默认3306
)
cursor = db.cursor()      ##获取游标
cursor.execute("select * from info")  ##执行命令
data = cursor.fetchone()   ##获取数据
print(data)
db.close()  ##关闭数据库

插入数据

import pymysql

db = pymysql.connect(
    host = "localhost",
    user = "root",
    password = "yuanming88",
    db = "text",
    port = 3306
)
cursor = db.cursor()
sql = """
insert into info(id,name,age,hometown) values(02,'aa',18,"Chengduo")
"""
cursor.execute(sql)
db.commit()   ##必须要提交
db.close()

另一种方法:

import pymysql

db = pymysql.connect(
    host = "localhost",
    user = "root",
    password = "yuanming88",
    db = "text",
    port = 3306
)
cursor = db.cursor()
sql = """
insert into info(id,name,age,hometown) values(%s,%s,%s,%s)
"""
cursor.execute(sql,(3,"bbb",21,"NanJing"))  ##如果不确定值,可以这样传入
db.commit()
db.close()

查找数据

有如下方法:

  1. fetchone():这个方法每次只会获取一条数据
import pymysql

db = pymysql.connect(
    host = "localhost",
    user = "root",
    password = "yuanming88",
    db = "text",
    port = 3306
)
cursor = db.cursor()
cursor.execute("select * from info")
data = cursor.fetchone()
print(data)
db.close()
'''
result:
(1, 'Mingle', 20, 'MeiShan')
'''
  1. fetchall():会接收的所有返回结果
import pymysql

db = pymysql.connect(
    host = "localhost",
    user = "root",
    password = "yuanming88",
    db = "text",
    port = 3306
)
cursor = db.cursor()
cursor.execute("select * from info")
data = cursor.fetchall()
print(data)
db.close()
'''
result:
((1, 'Mingle', 20, 'MeiShan'), (2, 'aa', 18, 'Chengduo'), (3, 'bbb', 21, 'NanJing'))
'''
  1. fetchmany(n):可以返回指定数量的数据
import pymysql

db = pymysql.connect(
    host = "localhost",
    user = "root",
    password = "yuanming88",
    db = "text",
    port = 3306
)
cursor = db.cursor()
cursor.execute("select * from info")
data = cursor.fetchmany(2)
print(data)
db.close()
'''
reslut:
((1, 'Mingle', 20, 'MeiShan'), (2, 'aa', 18, 'Chengduo'))
'''

删除数据

import pymysql

db = pymysql.connect(
    host = "localhost",
    user = "root",
    password = "yuanming88",
    db = "text",
    port = 3306
)
cursor = db.cursor()
cursor.execute("delete from info where id=3")
db.commit()
db.close()

更新数据

import pymysql

db = pymysql.connect(
    host = "localhost",
    user = "root",
    password = "yuanming88",
    db = "text",
    port = 3306
)
cursor = db.cursor()
cursor.execute("update info set name = 'cc' where id = 1")
db.commit()
db.close()

MongoDBpymongo

建立连接

import pymongo
client = pymongo.MongoClient("localhost",27017) ##传入地址和端口就够了
db_list = client.list_database_names() ##该方法是打印数据库列表
print(db_list)

插入

  1. insert_one方法
import pymongo
client = pymongo.MongoClient("localhost",27017)
mydb = client['test'] ##先获取该集合
mycol = mydb['subtest']
mydict = {
    'name':'MM',
    'age':13,
    'hometown':'BeiJing'
}
mycol.insert_one(mydict)
  1. insert_many方法
import pymongo
client = pymongo.MongoClient("localhost",27017)
mydb = client['test']
mycol = mydb['subtest']
mydict = [
    {'name':"YY",'age':19,'hometown':'ShangHai'},
    {'name':'BB','age':20,'hobby':"reading"},
    {'name':'GG','age':16,'sex':'male'}
]
mycol.insert_many(mydict)

当然,我们也可以自己指定_id

数据查询

  1. find_one方法
import pymongo
client = pymongo.MongoClient("localhost",27017)
mydb = client['test']
mycol = mydb['subtest']
rep = mycol.find_one()
print(rep)
#{'_id': ObjectId('5cb474f9839d11a1a366c66b'), 'name': 'yuan'}
  1. find()方法
    该方法是查询集合中所有的数据
import pymongo
client = pymongo.MongoClient("localhost",27017)
mydb = client['test']
mycol = mydb['subtest']
rep = mycol.find()
for r in rep:
    print(r)
'''
result:
{'_id': ObjectId('5cb474f9839d11a1a366c66b'), 'name': 'yuan'}
{'_id': ObjectId('5cb47545839d11a1a366c66c'), 'name': 'LiSa', 'age': 18.0, 'hometown': 'Chengduo'}
{'_id': ObjectId('5cb4921534ddc820d0822c79'), 'name': 'MM', 'age': 13, 'hometown': 'BeiJing'}
{'_id': ObjectId('5cb4941034ddc83720aeb5a7'), 'name': 'YY', 'age': 19, 'hometown': 'ShangHai'}
{'_id': ObjectId('5cb4941034ddc83720aeb5a8'), 'name': 'BB', 'age': 20, 'hobby': 'reading'}
{'_id': ObjectId('5cb4941034ddc83720aeb5a9'), 'name': 'GG', 'age': 16, 'sex': 'male'}
'''

筛选

import pymongo
client = pymongo.MongoClient("localhost",27017)
mydb = client['test']
mycol = mydb['subtest']
rep = mycol.find({},{'_id':0,'name':1,'age':1})
##1指定显示,反之这不现实,_id要单独设置关闭
for r in rep:
    print(r)
'''
result:
{'name': 'yuan'}
{'name': 'LiSa', 'age': 18.0}
{'name': 'MM', 'age': 13}
{'name': 'YY', 'age': 19}
{'name': 'BB', 'age': 20}
{'name': 'GG', 'age': 16}
'''
  1. 根据条件查询
import pymongo
client = pymongo.MongoClient("localhost",27017)
mydb = client['test']
mycol = mydb['subtest']
rep = mycol.find({'age':20}) ##指定查询age为20的数据
for r in rep:
    print(r)
'''
result:
{'_id': ObjectId('5cb4941034ddc83720aeb5a8'), 'name': 'BB', 'age': 20, 'hobby': 'reading'}
'''
  1. 指定条数查询
import pymongo
client = pymongo.MongoClient("localhost",27017)
mydb = client['test']
mycol = mydb['subtest']
rep = mycol.find().limit(3) #指定只查询前3条数据
for r in rep:
    print(r)
'''
result:
{'_id': ObjectId('5cb474f9839d11a1a366c66b'), 'name': 'yuan'}
{'_id': ObjectId('5cb47545839d11a1a366c66c'), 'name': 'LiSa', 'age': 18.0, 'hometown': 'Chengduo'}
{'_id': ObjectId('5cb4921534ddc820d0822c79'), 'name': 'MM', 'age': 13, 'hometown': 'BeiJing'}
'''
  1. 修饰符查询
    • $all:匹配那些指定键的键值中包含数组,而且该数组包含条件指定数组的所有元素的文档。{field: { $all: [ <value> , <value1> ... ] }
    • $gt:匹配键值大于指定值的所有文档。{field: {$gt: value} }
    • $gte:匹配键值不小于指定值的所有文档。{field: {$gte: value} }
    • $lt:匹配键值小于指定值的所有文档。{field: {$lt: value} }
    • $lte:匹配键值不大于指定值的所有文档。{field: {$lte: value} }
    • $in:匹配键值等于指定数组中任意值的文档。{ field: { $in: [<value1>, <value2>, ... <valueN> ] } }
    • $nin:匹配键不存在或者键值不等于指定数组的任意值的文档。{ field: { $nin: [ <value1>, <value2> ... <valueN> ]} }
    • $ne:匹配键值不等于指定值的文档。{field: {$ne: value} }
    • $and:and指定一个至少包含两个表达式的数组,选择出满足该数组中所有表达式的文档。{ $and: [ { <expression1> }, { <expression2> } , ... , { <expressionN> } ] }
    • $nor:$nor执行逻辑NOR运算,指定一个至少包含两个表达式的数组,选择出都不满足该数组中所有表达式的文档。{ $nor: [ { <expression1> }, { <expression2> }, ... { <expressionN> } ] }
    • $not:$not执行逻辑NOT运算,选择出不能匹配表达式的文档 ,包括没有指定键的文档。{ field: { $not: { <operator-expression> } } }
    • $or:$or执行逻辑OR运算,指定一个至少包含两个表达式的数组,选择出至少满足数组中一条表达式的文档。{ $or: [ { <expression1> }, { <expression2> }, ... , { <expressionN> } ] }
import pymongo
client = pymongo.MongoClient("localhost",27017)
mydb = client['test']
mycol = mydb['subtest']
rep = mycol.find({"age":{'$lte':16}})
for r in rep:
    print(r)
'''result:
{'_id': ObjectId('5cb4921534ddc820d0822c79'), 'name': 'MM', 'age': 13, 'hometown': 'BeiJing'}
{'_id': ObjectId('5cb4941034ddc83720aeb5a9'), 'name': 'GG', 'age': 16, 'sex': 'male'}
'''

正则表达式查询

import pymongo
client = pymongo.MongoClient("localhost",27017)
mydb = client['test']
mycol = mydb['subtest']
rep = mycol.find({"name":{'$regex':'^[yY]'}}) #我们需要使用$regex来指定
for r in rep:
    print(r)

方法一:使用update_one,修改一条数据

import pymongo
client = pymongo.MongoClient("localhost",27017)
mydb = client['test']
mycol = mydb['subtest']
upd = {'name':'yuan'}  ##设定修改的范围条件
upd_v = {'$set':{'name':'Zz'}}  ##使用$set关键字修改
mycol.update_one(upd,upd_v)

方法二:使用update_many,修改多条信息

import pymongo
client = pymongo.MongoClient("localhost",27017)
mydb = client['test']
mycol = mydb['subtest']
upd = {'name':{'$regex':'^[yY]'}} ##设定修改的范围条件
upd_v = {'$set':{'age':22}}
mycol.update_one(upd,upd_v)

删除

方法一:我们可以使用delete_one()方法来删除一个文档,该方法第一个参数为查询对象,指定要删除哪些数据。

import pymongo
client = pymongo.MongoClient("localhost",27017)
mydb = client['test']
mycol = mydb['subtest']
de_v = {"name":"Zz"}  ##指定删除的条件
mycol.delete_one(de_v)

方法二:我们可以使用delete_many来删除多个值

import pymongo
client = pymongo.MongoClient("localhost",27017)
mydb = client['test']
mycol = mydb['subtest']
de_v = {"name":{"$regex":"^[BG]"}}
mycol.delete_many(de_v)

删除集合

import pymongo
client = pymongo.MongoClient("localhost",27017)
mydb = client['test']
mycol = mydb['subtest']
mycol.drop()

排序

sort()方法第一个参数为要排序的字段,第二个字段指定排序规则,1为升序,-1为降序,默认为升序。

import pymongo
 
myclient = pymongo.MongoClient("localhost",27017)
mydb = myclient["test"]
mycol = mydb["subtest"]
mydoc = mycol.find().sort("alexa", -1)
for x in mydoc:
  print(x)

猜你喜欢

转载自www.cnblogs.com/MingleYuan/p/10719968.html
0条评论
添加一条新回复