博客
关于我
Python 资源篇----参考《从Python开始学编程》
阅读量:222 次
发布时间:2019-02-28

本文共 2079 字,大约阅读时间需要 6 分钟。

文件篇
文件操作是编程中常见的基本操作。在Python中,我们可以通过文件对象来进行文件的读写和关闭。在处理文件时,建议使用上下文管理器来确保文件能够自动关闭,避免资源泄漏。
f = open("hello.txt", "r") # 打开文件进行读取content = f.read(10) # 读取10个字节的内容content = f.readline() # 读取一行内容content = f.readlines() # 读取所有行内容

f.write("I like apple\r\n") # 写入文件,注意换行符依赖于操作系统f.close() # 手动关闭文件

上下文管理器
上下文管理器是一种简洁的资源管理方式,适用于文件、数据库连接等资源。在Python中,通过
with语句可以自动管理资源的生命周期。文件操作中,使用上下文管理器可以避免手动关闭文件,代码更加简洁。
with open("file.txt", "w") as f:f.write("Hello World!")print(f.closed) # 输出True,表示文件已关闭
pickle包
pickle是一个强大的序列化库,能够将Python对象转换为字节流文件,便于存储和传输。以下是使用pickle的示例代码:
import pickle

class Bird(object):have_feather = Truereproduction_method = "egg"

保存对象

with open("bird.pkl", "wb") as f:pickle.dump(summer, f)

加载对象

with open("bird.pkl", "rb") as f:summer = pickle.load(f)print(summer.have_feather) # 输出True

time时间篇
datetime包用于时间处理,提供了丰富的时间操作功能。以下是时间操作的示例:
import datetime

获取当前时间

now = datetime.datetime.now()print(now) # 输出当前时间

测量程序运行时间

start = datetime.datetime(2012, 9, 3, 21, 30)for _ in range(100000):passend = datetime.datetime(2012, 9, 3, 21, 30)print(end - start) # 输出2秒

时间间隔运算

delta = datetime.timedelta(days=2)end_date = start + deltaprint(end_date - start) # 输出2天

日期格式转换

date_str = "2012-09-05-00-00-00"date = datetime.datetime.strptime(date_str, "%Y-%m-%d-%H-%M-%S")print(date.strftime("%Y-%m-%d %H:%M")) # 输出2012-09-05 00:00:00

正则表达式篇
正则表达式是一种强大的字符串匹配工具,通过预定义模式来搜索字符串。以下是使用正则表达式的示例:
import re

搜索模式

pattern = r"[0-9]"match = re.search(pattern, "abcd4ef56")print(match.group(0)) # 输出4print(match.group()) # 输出4

替换操作

replace_pattern = re.sub("[0-9]", "love", "abcd4ef56")print(replace_pattern) # 输出abcdloveflove

分割操作

split_pattern = re.split("[0-9]", "abcd4ef56")print(split_pattern) # 输出['abcd', 'ef', '', '']

查找所有匹配

findall_pattern = re.findall("[0-9]", "abcd4ef56")print(findall_pattern) # 输出['4', '5', '6']

网络篇
使用http.client库可以在Python中进行简单的网络请求。以下是发送GET请求的示例:
import http.client

conn = http.client.HTTPConnection("www.example.com")conn.request("GET", "/")response = conn.getresponse()print(response.status, response.reason) # 输出200 OK

读取响应内容

content = response.read()print(content)

转载地址:http://fzzp.baihongyu.com/

你可能感兴趣的文章
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MYSQL8.0以上忘记root密码
查看>>
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>
MySQL8,体验不一样的安装方式!
查看>>
MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
查看>>
Mysql: 对换(替换)两条记录的同一个字段值
查看>>
mysql:Can‘t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘解决方法
查看>>
MYSQL:基础——3N范式的表结构设计
查看>>