首页 > 编程语言 >python 好用的库

python 好用的库

时间:2022-12-11 00:44:25浏览次数:48  
标签:count apple python db John type 好用 name

Prerequisite

包含:arrow、tinydb

arrow

arrow 可以很方便的处理时间和日期

import arrow

now = arrow.now()
print(now)
# 2022-12-11T00:25:59.424262+08:00
year = now.format('YYYY')
print("Year: {0}".format(year))
# Year: 2022
date = now.format('YYYY-MM-DD')
print("Date: {0}".format(date))
# Date: 2022-12-11
date_time = now.format('YYYY-MM-DD HH:mm:ss')
print("Date and time: {0}".format(date_time))
# Date and time: 2022-12-11 00:25:59
date_time_zone = now.format('YYYY-MM-DD HH:mm:ss ZZ')
print("Date and time and zone: {0}".format(date_time_zone))
# Date and time and zone: 2022-12-11 00:25:59 +08:00

tinydb

tinydb 是一个轻量级数据库,和 MongoDB 类似是处理 json 文件,但只需要 pip 一下即可使用,非常方便

from tinydb import *

db = TinyDB('db.json')
# 如果没有会自动创建

db.insert({'type': 'apple', 'count': 10})
db.insert({'type': 'banana', 'count': 20})
# [{'type': 'apple', 'count': 10}, {'type': 'banana', 'count': 20}]
# 单条创建

db.insert_multiple([
    {'name': 'John', 'age': 22},
    {'name': 'John', 'age': 37}])
# [{'type': 'apple', 'count': 10}, {'type': 'banana', 'count': 20}, {'name': 'John', 'age': 22}, {'name': 'John', 'age': 37}]
# 多条创建

print(db.all())
# [{'type': 'apple', 'count': 10}, {'type': 'banana', 'count': 20}, {'name': 'John', 'age': 22}, {'name': 'John', 'age': 37}]
# 查看全部数据

Fruit = Query()
print(db.search(Fruit.type == 'apple'))
# [{'type': 'apple', 'count': 10}]
# 查看特定数据

db.update({'type': 'apple', 'count': 50})
print(db.search(Fruit.type == 'apple'))
# [{'type': 'apple', 'count': 50}, {'type': 'apple', 'count': 50}, {'name': 'John', 'age': 22, 'type': 'apple', 'count': 50}, {'name': 'John', 'age': 37, 'type': 'apple', 'count': 50}]
# 更新数据

db.remove(Fruit.count < 40)
print(db.all())
# [{'type': 'apple', 'count': 50}, {'type': 'apple', 'count': 50}, {'name': 'John', 'age': 22, 'type': 'apple', 'count': 50}, {'name': 'John', 'age': 37, 'type': 'apple', 'count': 50}]
# 删除某条数据

db.truncate()
print(db.all())
# []
# 清空数据库

标签:count,apple,python,db,John,type,好用,name
From: https://www.cnblogs.com/CourserLi/p/16972699.html

相关文章