我将数据库中的一百二十一万一千二百五十六条数据的时间格式转换成时间戳用时1分半
hold on 气质这一块还没跟上 现在我只想说一句话!!!!
最近一直在做数据整理这一块的工作(就是将公司之前放在文档中的数据整理一下存到数据库中进行数据的实时查看)
终于在我费尽千幸万苦将数据妥妥的放到数据库中时,还有最后一项工作就可以完美收官了!(就是将日期转换成时间戳格式,刚开始还没想那么多,但是一想100多瓦条数据,这要是用接口数据来写这是不是太慢了。。。)然后就想到了python
ok,那废话不多说 直接本主题 ~
需求分析
根据我们的需求,可以将数据转换成时间戳分为两个步骤,数据库数据读取和时间转换成时间戳这两步操作
一、数据库数据读取(增删改查)
1.安装 pymysql 第三方库
点击左上角的setting ->加号->pymysql->install->ok
连接测试
#导入库
import pymysql
# 打开数据库连接
#host 服务地址
#user 用户名
#password 密码
#db 数据库
#port 端口
db = pymysql.connect(host="localhost", user="xxxx",
password="xxxx", db="xxxx", port=3306)
# 使用cursor()方法获取操作游标
cur = db.cursor()
如果报错了的话 看报错信息 要记得你的账号密码哟!
一定要将自动检测设置打开
查询操作
这里的正删改查就相对与很简单了 就不一一解释了
import pymysql #导入 pymysql
#打开数据库连接
db= pymysql.connect(host="localhost",user="xxxx",
password="xxxx",db="xxxx",port=3306)
# 使用cursor()方法获取操作游标
cur = db.cursor()
#1.查询操作
# 编写sql 查询语句 user 对应我的表名
sql = "select * from user"
try:
cur.execute(sql) #执行sql语句
results = cur.fetchall() #获取查询的所有记录
print("id","name","password")
#遍历结果
for row in results :
id = row[0]
name = row[1]
password = row[2]
print(id,name,password)
except Exception as e:
raise e
finally:
db.close() #关闭连接
插入操作
import pymysql
#2.插入操作
db= pymysql.connect(host="xxxx",user="xxxx",
password="xxxx",db="xxxx",port=3306)
# 使用cursor()方法获取操作游标
cur = db.cursor()
sql_insert ="""insert into user(id,username,password) values(4,'liu','1234')"""
try:
cur.execute(sql_insert)
#提交
db.commit()
except Exception as e:
#错误回滚
db.rollback()
finally:
db.close()
更新操作
更新操作
db= pymysql.connect(host="localhost",user="root",
password="123456",db="test",port=3307)
# 使用cursor()方法获取操作游标
cur = db.cursor()
sql_update ="update user set username = '%s' where id = %d"
try:
cur.execute(sql_update % ("xiongda",3)) #像sql语句传递参数
#提交
db.commit()
except Exception as e:
#错误回滚
db.rollback()
finally:
db.close()
删除操作
import pymysql
#4.删除操作
db= pymysql.connect(host="localhost",user="root",
password="123456",db="test",port=3307)
# 使用cursor()方法获取操作游标
cur = db.cursor()
sql_delete ="delete from user where id = %d"
try:
cur.execute(sql_delete % (3)) #像sql语句传递参数
#提交
db.commit()
except Exception as e:
#错误回滚
db.rollback()
finally:
db.close()
二、时间转换为时间戳
这里就是我在网上找到的一个时间戳转换方法体
def timeformat_to_timestamp(timeformat=None, format='%Y-%m-%d'):
if timeformat:
time_tuple = time.strptime(timeformat, format)
res = time.mktime(time_tuple) # 转成时间戳
else:
res = time.time() # 获取当前时间戳
return int(res)
三、正式实施
ok,到这里我们的准备工作算是结束了 下面正式开始数据读取转换(因数据涉及到公司信息 所以只展示要转换的部分时间数据 )
这里我的时间有点不太一样 来看一下吧(chukuriqi)
我的数据是格式分割不是用-而是用.来分割 并且前面没有20XX.XX.XX格式 所以我在插入之前要对我的数据进行稍微变化一下
然后转换过的时间戳数据我新建了一个列来存放我的转换成时间戳的格式数据 (chukutime)
思路:
首先我要读取数据库表中的所有数据 ,然后在循环遍历,分别拿到我的要转换成时间戳的字段(chukuriqi) 以及转换成时间戳存储到的字段中(chukutime) ,所以我也要拿到 id进行更细操作
剩下的就可以交给编码了
import pymysql
import time
#时间转换为时间戳方法体 其中因为我的日期格式特殊 所以将%Y-%m-%d 换为 %Y.%m.%d
def timeformat_to_timestamp(timeformat=None, format='%Y.%m.%d'):
try:
if timeformat:
time_tuple = time.strptime(timeformat, format)
res = time.mktime(time_tuple) # 转成时间戳
else:
res = time.time() # 获取当前时间戳
#因我的数据库中这要转换时间字段中可能会有格式不正确的这里直接处理抛出异常 直接将他转换为当前时间
except Exception:
res = time.time() # 获取当前时间戳
finally:
return int(res)
# 打开数据库连接
db = pymysql.connect(host="localhost", user="xxxx",
password="xxxx", db="xxxx", port=3306)
# 使用cursor()方法获取操作游标
cur = db.cursor()
# 1.查询操作
# 编写sql 查询语句 user 对应我的表名
sql = "select * from 表名"
try:
cur.execute(sql) # 执行sql语句
sql_update = "update 表名 set chukutime = '%s' where id = %d"
results = cur.fetchall() # 获取查询的所有记录
print("id", "chukuriqi", "chukutime")
# 遍历结果
for row in results:
id = row[0]
chukuriqi = row[2]
if row[5] == '':
print(id, chukuriqi)
#读取每一条数据时在前面增加20 使时间格式完整!
chukutime = timeformat_to_timestamp('20' + str(chukuriqi))
cur.execute(sql_update % (chukutime, id)) # 像sql语句传递参数
print('时间转换中')
else:
print('loading')
else:
#更新数据表中数据
db.commit()
print('全部加载完成')
except Exception as e:
raise e
finally:
#关闭连接
db.close()
ok,这就可以和我一样快了 哈哈哈哈哈哈
详细的注释请看代码
编写不易,希望看到这里的小可爱能够点一波赞 谢谢啦!!!