Python常忘内容总结

1.获取时间

特定时间格式:

from datetime import datetime
ISOTIMEFORMAT = "%Y-%m-%d %H:%M:%S" #时间格式,可修改为其他格式
now = datetime.now().strftime(ISOTIMEFORMAT)

时间戳:

import time
timestamp = time.time() #timestamp为从1970年1月1日00:00:00(UTC)到现在的秒数,小数点后为毫秒微秒
print(round(timestamp*1000)) #获取毫秒,四舍五入
#int(num),int()直接截断整数部分

2.数据库连接及使用

连接mysql数据库并执行SQL语句:

lon = data.get("longitude")
lat = data.get("latitude")
speed = data.get("speed")
ISOTIMEFORMAT = "%Y-%m-%d %H:%M:%S"
now = datetime.now().strftime(ISOTIMEFORMAT)
sql1 = "delete from gps_realtime where device_id=%s;"
sql = "insert into gps_realtime(device_id,gps_time,longitude,latitude,speed) values(%s,%s,%s,%s,%s);"
print(sql)
conn = pymysql.connect(
    host="",
    port=3306,
    user="",
    passwd="",
    db="",
    charset="utf8",
    cursorclass=pymysql.cursors.DictCursor,
)
# 这里进行数据库操作
cursor = conn.cursor()
values = ()
cursor.execute(sql1, ("1111"))
cursor.execute(sql, values)
conn.commit() #重点,需要提交才会执行语句,select时不需要,insert,delete需要
cursor.close()
conn.close()

获取数据Json格式:

result = cursor.fetchall()

3.Decimal:高精度的十进制运算

from decimal import Decimal
#可以传递给Decimal整型或者字符串参数,但不能是浮点数据,因为浮点数据本身就不准确。
x = Decimal('3.14') #赋值
y = Decimal(3.14) #实际输出y=3.140000000000000124344978758017532527446746826171875
z = Decimal('1e-3')

a = Decimal("1.23") + Decimal("4.56")
b = Decimal("1.23") - Decimal("4.56")
c = Decimal("1.23") * Decimal("4.56")
d = Decimal("1.23") / Decimal("4.56")

rounded = Decimal("1.23").quantize(Decimal('0.00')) # 对1.23四舍五入到小数点后两位

a = Decimal("1.23")
b = Decimal("4.56")
if a < b:
    print("a小于b")
else:
    print("a大于等于b")

x = Decimal("3.14")
print(x.to_integral())  # 输出: 3
print(x.to_sci_string())  # 输出: 3.14e+00

4.JSON数据

解析JSON数据:

import json
json_data = '{"name": "John", "age": 30, "city": "New York"}' #数据{}里不能有单引号'
data = json.loads(json_data)

print(data)  # 输出:{'name': 'John', 'age': 30, 'city': 'New York'}

生成JSON数据:

import json
data = {'name': 'John', 'age': 30, 'city': 'New York'}
json_data = json.dumps(data)
print(json_data)  # 输出:'{"name": "John", "age": 30, "city": "New York"}'

写入JSON文件:

import json
data = {'name': 'John', 'age': 30, 'city': 'New York'}
with open('data.json', 'w') as f:
    json.dump(data, f)

读取JSON文件:

import json
with open('data.json', 'r') as f:
    data = json.load(f)
print(data)  # 输出:{'name': 'John', 'age': 30, 'city': 'New York'}

Redis

redis连接及存取数据:

import redis
import json
# 创建Redis连接
r = redis.Redis(host='localhost', port=6379, db=0,password="ddd")

# JSON数据
json_data = {'name': 'Alice', 'age': 30, 'city': 'New York'}

# 将JSON数据序列化为字符串并存储
r.set('user1', json.dumps(json_data))
# 获取数据
r.get('user1')
r.close()
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇