一、时间模块
import time as tm
from datetime import datetime, time, date, timedelta
1.datetime
1)获取当前时间
ti = datetime.now()
print(ti)
2)创建datetime对象(时间对象)
t2 = datetime(2002, 10, 1)
print(t2) # 2002-10-01 00:00:00
t3 = datetime(2012, 9, 3, minute=30)
print(t3) # 2012-09-03 00:30:00
3)单独获取时间数据
print(t2.year)
print(t2.month)
print(t2.day)
4)时间对象和字符串时间的转换
将字符串时间转换成时间对象:datetime.strptime(字符串时间, 时间格式)
t4_str = '2001-8-3 11:32:40'
t4 = datetime.strptime(t4_str, '%Y-%m-%d %H:%M:%S')
print(t4, t4.weekday()) # 2001-08-03 11:32:40 4
t5_str = '2011/6/6 10:20:47'
t5 = datetime.strptime(t5_str, '%Y/%m/%d %H:%M:%S')
print(t5)
5)将时间对象转换成字符串
t7 = datetime(2020, 1, 5, 22, 54, 13)
print(t7)
t7_str = t7.strftime('%Y年%m月%d日 %H时%M分%S秒')
print(t7_str)
2.timedelta - 对时间进行加减操作的
t8 = datetime(2020, 12, 31, 23, 59, 10)
加1天
result = t8 + timedelta(days=1)
print(result) # 2021-01-01 23:59:10
练习:
def get_timestamp(t: str, format = '%Y-%m-%d %H:%M:%S'):
import time
struct_t = time.strptime(t, '%Y-%m-%d %H:%M:%S')
return time.mktime(struct_t)
二、文件操作
1.数据持久化(数据本地化)
'''
1)基础理论
程序中的数据默认是保存在运行内存中的,保存在运行内存中的数据在程序运行结束后悔被自动销毁。
保存在硬盘、磁盘中的数据在程序结束后不会销毁。
数据持久化 - 将数据以文件为单位保存在硬盘中。
2)常见数据持久化的工具
数据库(.db、.sqlite)、excel文件、csv文件、plist文件(.plist)、json文件(.json)、txt文件(.txt)
'''
文件操作
'''
文件操作基本流程:打开文件 -> 操作文件(读、写) -> 关闭文件
'''
1)打开文件
'''
open(文件路径,打开方式,encoding=文件编码方式) - 以指定的方式打开指定的文件,并返回一个文件对象
a.文件路径 - 字符串;用来确定需要打开的是哪个文件
绝对路径:文件在计算机中的全路径,windows系统一般以某个盘的名字开头
相对路径:1)用.表示当前目录
2)用..表示当前目录的上层目录
注:当前目录指的是当前代码文件所在的目录;相对路径中最前面的.可以省略
b.打开方式 - 字符串;决定打开文件后能读还是能写;决定读写的数据类型是字符串还是二进制。
1)第一组值:决定读写的
'r' - 只读;
'w' - 只写;打开会清空原文件
'a' - 只写;
2)第二组值:决定数据类型
'b' - 二进制
't' - 字符串(文本)
给打开方式赋值时需要在两组值中每一组选一个值组合:rt、rb、br、wb、tw...
注意:如果以读的方式打开不存在的文件,程序会报错!如果以写的方式打开不存在的文件,程序不会报错并且会自动创建这个文件
c.encoding - 文本文件的编码方式,常用的值是'utf-8'
如果打开文件的时候设置的编码方式和文件本身的编码方式不一致,就会报编码错误
注意:如果是以带b方式打开的文件,都不能给encoding赋值
'''
绝对路径
open(r'D:qianfeng 1语言基础day15-时间模块和文件操作filesdemo1.txt')
相对路径
open(r'.filesdemo1.txt')
r - 只读
f = open(r'filesdemo1.txt', 'r')
f.read()
f.write('ass') # io.UnsupportedOperation: not writable
w - 只写
f = open(r'filesdemo1.txt', 'w')
f.write('hello')
a - 只写;不会清空
f = open(r'filesdemo1.txt', 'a')
f.write('ok')
t - 字符串
f = open(r'filesdemo1.txt', 'rt')
f.read()
b - 二进制(bytes)
f = open(r'filesdemo1.txt', 'rb')
result = f.read()
print(type(result)) #
f = open(r'filesdemo1.txt', 'wb')
f.write(b'123')
f = open(r'filesdemo1.txt', 'w')
f.write('process')
文件不存在
open(r'filesdemo2.txt', 'r') # FileNotFoundError
open(r'filesdemo2.txt', 'w')
open(r'filesdemo3.txt', 'a')
open(r'C:UsersAdminDesktop新建文本文档.txt', encoding='utf-8')
2)文件读写操作
'''
a. 读 - 获取文件内容
文件对象.read() - 从读写位置开始,读到文件结尾
文件对象.readline() - 从读写位置开始,读到一行的结尾(只针对以t方式打开的文件有效)
b. 写 - 将文件内容写入到文件中
文件对象.write(内容) - 将指定内容写入到文件中
'''
f = open(r'filesdemo1.txt')
result = f.read()
print(result)
f.seek(0) # 将读写位置移动到文件开头
增
f = open(r'filesdemo1.txt', 'a')
f.write('nccc')
插入
f = open(r'filesdemo1.txt')
result = f.read()
lines = result.split('n')
# lines.insert(1, '555') # 插入一行
lines[1] = 'AAA' # 替换修改一行
write_data = 'n'.join(lines)
f = open(r'filesdemo1.txt', 'w')
f.write(write_data)
3)关闭文件
文件对象.close()
f.close()
f.write('abc') # ValueError: I/O operation on closed file.
'''
with open(文件路径,打开方式,encoding=编码方式)as 文件对象:
文件上下文(离开上下文,文件自动关闭)
'''
三、数据持久化的方法
1.数据持久化的方法
'''
第一步:确定需要持久化的数据
第二步:创建文件保存数据(确定需要持久化的数据的初始值)
第三步:程序中需要数据的时候从文件中读数据
第四步:数据如果发生改变,要将最新的数据写入文件中
'''
写程序打印程序执行的次数
f = open(r'filesdemo2.txt')
result = int(f.read())
result_new = str(result + 1)
f = open(r'filesdemo2.txt', 'w')
count = f.write(result_new)
print(result)
练习:完成注册的功能;要求下次运行程序的时候可以看到上一次注册的账号
请输入账号:
请输入密码:
提示注册成功! / 提示注册失败!(如果这个账号之前已经注册过了,就注册失败)
{账号1:密码1, 账号2:密码2, …}
user = input('请输入账号:')
password = input('请输入密码:')
f1 = open(r'filesdemo3.txt')
result = eval(f1.read())
if user in result:
print('注册失败!')
else:
result[user] = password
f = open(r'filesdemo3.txt', 'w')
f.write(str(result))
print('注册成功!')