- 打开文件
- 读取文件
- 关闭文件
- 结果
- 打开文件(以文本打开)
- 读取文件
- 关闭文件
- 结果
- [(‘character-set-client-handshake’, ‘FALSE’), (‘character-set-server’, ‘utf8mb4’), (‘collation-server’, ‘utf8mb4_general_ci’), (‘init_connect’, “‘SET NAMES utf8mb4’”), (‘port’, ‘3306’), (‘socket’, ‘/data/mysql.sock’), (‘key_buffer_size’, ‘16M’), (‘max_allowed_packet’, ‘1M’), (‘table_open_cache’, ‘64’), (‘sort_buffer_size’, ‘512K’)]
- 判断节点是否存在
- 追加节点
- 追加节点数据
- 删除节点数据
- 安装第三方模块
- 读sheet
- 循环所有sheet,读取每一个sheet的第一行第二列
- 文件路径处理
- 用户注册
- 用户登录
- 文件路径处理
- 创建excel
- 解析ini文件
- 循环获取每个节点
1) 文件操作
- 字符编码
字符串类型(str)在程序中用于表示文字信息,本质上是unicode编码的二进制
字节类型(bytes),本质上utf-8/gbk编码,表示原始二进制(图片,文件等信息)
name = "熊纹理"
data = name.encode('utf-8') ---字节
print(data)
result = data.decode('utf-8') --字符串
print(result)
1.1)读文件
- 读文本文件
注意事项:
- 关于路径,可以相对路径,也可以绝对路径
- windows系统中写绝对路径冗余出问题,用r”c:\new\info.txt”
- 读文件时,文件不存在程序会报错
```bashimport os
exists = os.path.exists("info.txt")
print(exists) #存在就返回true
if exists:
file_object = open('info.txt',mode='rt',encoding='utf-8')
data = file_object.read()
file_object.close()
print(data)
else:
print('文件不存在')
打开文件
file_object = open(‘info.txt’,mode=’rb’)读取文件
data = file_object.read()关闭文件
file_object.close() print(data)结果
b’hello world\r\n\xe6\x88\x91\xe6\x9d\xa5\xe8\x87\xaa\xe6\xb1\x9f\xe8\xa5\xbf\r\nziioffice’
print(data.decode()) (由字节类型转为字符串类型)
打开文件(以文本打开)
file_object = open(‘info.txt’,mode=’rt’,encoding=’utf-8’)
读取文件
data = file_object.read()
关闭文件
file_object.close() print(data)
结果
hello world 我来自江西 ziioffice
```bash
读一张图片
#打开文件
file_object = open('a1.jpg',mode='rb')
#读取文件
data = file_object.read()
#关闭文件
file_object.close()
print(data)
模式详解
#只读 r,rb,rt
存在,读
不存在,报错
#只写 w,wb,wt
存在,清空在写
不存在,创建在写
#只写 x,xb,xt (几乎不用)
存在: 报错
不存在: 创建在写
#只写 a,ab,at (尾部追加)
存在: 尾部追加
不存在: 创建在写
#读写
r+ rb+ 默认光标起始位置
w+ wb+ 默认光标起始位置,清空文件
a+ ab+ 默认光标起始位置,不清空文件(追加)
r: 读
w: 写(会先清空文件,在写)
a: 写(追加写)
b: 二进制
t: 文本
file_object = open('info1.txt',mode='rt+')
data = file_object.read()
print(data)
file_object.write("aaa")
print(data)
file_object.close()
#读所有
read() #常用操作
#读n个字符
read(2)
#读一行
readline()
#读所有行
readlines() #写到列表
#循环读 (常用操作)
file = open("info1.txt",mode="r")
for line in file:
print(line.strip())
file.close()
1.2) 写文件
写文本文件
#打开文件
file_object = open('info1.txt',mode='wb')
#写文件
file_object.write('兄云'.encode('utf-8'))
#关闭
file_object.close()
#打开文件
file_object = open('info1.txt',mode='wt',encoding='utf-8') #默认为utf-8
#写文件
file_object.write('兄云')
#关闭
file_object.close()
写图片等文件 ```bash f1=open(“a1.png”,mode=”rb”) content=f1.read() f1.close()
file_object=open(“a2.png”,mode=”wb”) file_object.write(content) file_object.close()
```bash
user = input("请输入用户名:")
pwd = input("请输入密码:")
data = "{}-{}".format(user,pwd)
file_object = open("info1.txt",mode="wt")
file_object.write(data)
file_object.close()
#多用户注册
file_object = open("info1.txt",mode="wt")
while True:
user = input("请输入用户名:")
if user.upper() == "Q":
break
pwd = input("请输入密码:")
data = "{}-{}\n".format(user,pwd)
file_object.write(data)
file_object.close()
#如何利用python向某个网站发送请求获取数据
#下载第三方模块
(venv) E:\python>pip install requests
#使用第三方模块
import requests
res = requests.geturl="网址"
案例1:
import requests
res = requests.get(
url="https://www.php.net/manual/zh/class.commonmark-node-document.php",
headers={
"User-Agent": "User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
}
)
file_object = open("info2.txt",mode="wb")
file_object.write(res.content)
file_object.close()
案例2: 下载一张图片
import requests
res = requests.get(
url="https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1887089220,2670743537&fm=26&gp=0.jpg",
headers={
"User-Agent": "User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
}
)
file_object = open("a1.jpg",mode="wb")
file_object.write(res.content)
file_object.close()
注意事项:
- 关于路径,支持绝对路径和相对路径
- 当文件不存在,w模式会新建在写入内容
- 当文件存在,w模式会清空文件
1.3)游标
#flush 刷新硬盘
#移动光标 seek() 字节,(一个汉字3个字节)
r模式: 光标移动到指定字节的位置
a模式: 移动光标没用,会永远写到文件尾部
#获取当前光标的位置 tell()
f = open('info1.txt',mode='r+',encoding="utf-8")
p1 = f.tell()
print(p1) # 0
f.read(3) # 这里3表示3个字符
p2 = f.tell()
print(p2) # 9
f.close()
1.4)with上下文管理 (非常重要)
之前对文件进行操作,每次都要打开和关闭文件,比较繁琐且容易关闭文件
with open('info1.txt',mode='rb') as f:
data = f.read()
print(data)
#结果
b'aaa'
with open支持同时打开多个文件
user_dict = {}
with open('files/access.log',mode='r',encoding='utf-8') as file:
for line in file:
user_ip = line.split(" ")[0]
if user_ip in user_dict:
user_dict[user_ip] += 1
else:
user_dict[user_ip] = 1
print(user_dict)
with open('info.conf', mode='r', encoding='utf-8') as f1, open('info1.conf', mode='w', encoding='utf-8') as f2:
for line in f1:
new_line = line.replace('aaa', 'a1')
f2.write(new_line)
import shutil
shutil.move('info1.conf', 'info.conf') # 重命名文件
2)csv格式文件
用固定的分隔符,分割数据,一般用逗号
import requests
import os
with open('file/mv.csv',mode='r',encoding='utf-8') as file_object:
file_object.readline()
for line in file_object:
user_id,username,url_address = line.strip().split(',')
print(username,url)
#根据url下载图片
res = request.get(
url = url_address,
headers={
"User-Agent": "User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
}
)
#将图片写入到文件 确保images文件夹存在
if not os.path.exists("images"):
#创建images目录
os.makedirs("images")
with open("images/{}.jpg".format(username),mode="wb") as img_object:
img_object.write(res.content)
3)ini格式文件
ini文件平常用于存储软件的配置文件,如下
[client]
port=3306
socket=/data/mysql.sock
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]
character-set-client-handshake=FALSE
character-set-server=utf8mb4
collation-server=utf8mb4_general_ci
init_connect='SET NAMES utf8mb4'
port=3306
socket=/data/mysql.sock
key_buffer_size=16M
max_allowed_packet=1M
table_open_cache=64
sort_buffer_size=512K
读取所有节点
#读取所有节点
import configparser
config=configparser.ConfigParser()
config.read('my.cnf',encoding='utf-8')
res=config.sections()
print(res)
读取节点下的值 ```python import configparser config=configparser.ConfigParser() config.read(‘my.cnf’,encoding=’utf-8’) res=config.sections() for node in res: data = config.items(node) print(data) #以元组的形式记录每一条键值
[(‘character-set-client-handshake’, ‘FALSE’), (‘character-set-server’, ‘utf8mb4’), (‘collation-server’, ‘utf8mb4_general_ci’), (‘init_connect’, “‘SET NAMES utf8mb4’”), (‘port’, ‘3306’), (‘socket’, ‘/data/mysql.sock’), (‘key_buffer_size’, ‘16M’), (‘max_allowed_packet’, ‘1M’), (‘table_open_cache’, ‘64’), (‘sort_buffer_size’, ‘512K’)]
- 获取节点下的键值
```python
import configparser
config=configparser.ConfigParser()
config.read('my.cnf',encoding='utf-8')
res=config.sections()
for node in res:
data = config.items(node)
for key,value in data:
print(key,value)
获取节点下键对应的值
import configparser
config=configparser.ConfigParser()
config.read('my.cnf',encoding='utf-8')
result = config.get('mysqld','init_connect')
print(result)
追加节点等其他操作 ```python
判断节点是否存在
config.has_section(‘client’)
追加节点
import configparser config=configparser.ConfigParser() config.read(‘my.cnf’,encoding=’utf-8’) config.add_section(‘groups’) config.write(open(‘my.cnf’,mode=’w’,encoding=’utf-8’))
追加节点数据
import configparser config=configparser.ConfigParser() config.read(‘my.cnf’,encoding=’utf-8’) if not config.has_section(‘groups’): config.add_section(‘groups’) config.set(‘groups’,’ip’,’172.19.156.142’) config.write(open(‘my.cnf’,mode=’w’))
删除节点数据
import configparser config=configparser.ConfigParser() config.read(‘my.cnf’,encoding=’utf-8’) config.remove_option(‘groups’,’name’) config.write(open(‘my.cnf’,mode=’w’))
4)xml格式文件
```python
from xml.etree import ElementTree as ET
#读取文件
tree = ET.parse('context.xml')
#打开根标签
root = tree.getroot()
print(root)
#读取节点数据
for child in root:
#print(child.tag,child.attrib)
for node in child:
print(node.tag,node.attrib,node.text)
#获取某一个标签
contry_object = root.find('contry')
print(contry_object.tag,contry_object.attribe)
rank_object=contry_object.find('rank')
print(rank_object.tag,rank_object.atrribe,rank_object.text)
#修改和删除节点
root = ET.XML(content)
rank = root.find('contry').find('rank')
rank.text = 999 #修改
rank.set('update','2020-11-11') #添加属性
tree = ET.ElementTree(root)
tree.write('new.xml',encoding='utf-8')
#删除
root.remove(root.find('contry').find('address'))
tree = ET.ElementTree(root)
tree.write('new.xml',encoding='utf-8')
5)excel文件
读sheet
from openpyxl import load_workbook wb = load_workbook(‘files/py.xlsx’) 1)读取excel文件中的所有sheet名称 print(wb.sheetnames) 2)选择sheet,基于sheet名称 sheet = wb[“第一季度”] cell = sheet.cell(1,2) #第一行第二列 print(cell.value) #打印该单元格值
3)选择sheet,基于索引 sheet = wb.worksheets[0] cell = sheet.cell(1,2) print(cell.value)
循环所有sheet,读取每一个sheet的第一行第二列
for name in wb.sheetnames: sheet1 = wb[name] cell = sheet1.cell(1,2) print(cell)
- 读单元格
```python
from openpyxl import load_workbook
wb = load_workbook('files/py.xlsx')
sheet = wb.worksheets[0]
cell = sheet.cell(1,1) #单元格第一行第一列
print(cell.value) #单元格的文本内容
#单元格的样式
print(cell.font) #字体
print(cell.style) #样式
print(cell.alignment) #排列清空
#获取具体位置的单元格
c1 = sheet['a2']
print(c1.value)
#获取N行所有的单元格
sheet[1] #获取第一行所有单元格 第一个元组
for cell in sheet[1]:
print(cell.value) #展示第一行的单元格内容
#获取所有行的单元格,获取某列数据
for row in sheet.rows:
print(row[0].value,row[1].value)
#获取所有列的数据
for col in sheet.columns:
print(col[0].value) #取每一列的第一行数据
6)压缩文件
7)案例
data_list = [11,22,34,45]
for i,item in enumerate(data_list,2):
print(i,item)
#结果
2 11
3 22
4 34
5 45
- 用户注册及登录
```python
文件路径处理
import os basedir = os.path.dirname(os.path.abspath(_file)) ob_file_path = os.path.join(base_dir,’registry.csv’)
用户注册
while True: choice = input(“是否进行用户注册(Y/N):”) choice = choice.upper() if choice not in {“Y”,”N”}: print(“输入格式错误,请重新输入:”) continue if choice == “N” or choice == “n”: break with open(ob_file_path,mode=’a’,encoding=’utf-8’) as file_object: while True: user = input(“请输入用户名(Q/q退出):”) if user.upper() == “Q”: break pwd = input(“请输入密码:”) file_object.write(“{},{}\n”.format(user,pwd)) file_object.flush()
用户登录
print(“欢迎使用**系统,请登录:”) username = input(“请输入用户名(Q/q退出): “) password = input(“请输入密码:”)
if not os.path.exists(ob_file_path): print(“用户文件不存在”) else: with open(ob_file_path,mode=’r’,encoding=’utf-8’) as file_object: for line in file_object: user,pwd = line.strip().split(“,”) if username == user and password == pwd: print(“登录成功”) else: print(“用户名或密码错误”)
2、获取天气存到excel
```python
#文件路径处理
import os
import requests
from xml.etree import ElementTree as ET
from openpyxl import workbook
#处理文件路径
base_dir = os.path.dirname(os.path.abspath(__file__))
ob_file_path = os.path.join(base_dir,'weather.xlsx')
#创建excel且默认会创建一个sheet
wb = workbook.Workbook()
del wb["Sheet"]
while True:
city = input("请输入城市(Q/q退出): ")
if city.upper() == "Q":
break
url = "http://ws.webxml.com.cn//WebServices/WeatherWebService.aspx/getWeatherbyCityName?theCityName={}".format(city)
res = requests.get(url=url)
#print(res.text)
#提取xml的值
root = ET.xml(res.text)
#创建sheet
sheet = wb.create_sheet(city)
for row_index,node in enumerate(root,1):
text = node.text
cell = sheet.cell(row_index,1)
cell.value = text
wb.write(ob_file_path)
- 解析ini文件,存到excel表格
```python
import os
import configparser
from openpyxl import workbook
from openpyxl.styles import Alignment,Border,Side,Font,PatternFill
文件路径处理
basedir = os.path.dirname(os.path.abspath(_file)) file_path = os.path.join(base_dir,”my.ini”) target_excel_file_path = os.path.join(base_dir,”my.xlsx”)创建excel
wb = workbook.Workbook() del wb[“Sheet”]
解析ini文件
config = configparser.ConfigParser() config.read(file_path,encoding=’utf-8’)
循环获取每个节点
for section in config.sections(): sheet = wb.create_sheet(section) side = Side(style=”thin”,color=”000000”) border = Border(top=side,bottom=side,left=side,right=side) align = Alignment(horizontal=”center”,vertical=”center”) title_dict = {“A1”: “键”, “B1”:”值”} for postition,text in title_dict.items(): cell = sheet[postition] cell.value = text cell.alignment = align cell.fill = PatternFill(“solid”,fgColor=”64951d”) cell.font = Font(name=”微软雅黑”, color=”FFFFFF”) cell.border = border row_index = 2 for group in config.items(section): for col,text in enumerate(group,1): cell = sheet.cell(row_index,col) cell.alignment = align cell.border = border cell.value = text row_index += 1 wb.save(target_excel_file_path) ```