文章目录
一、基础学习
1.1 输入功能
# name = input("请输入用户名:")
# if name == "hehe":
# print("sb")
# else:
# print("ss")
1.2 占位符
# name = "喵喵"
# age = 19
# text = f"嫂子的名字叫{name},年龄是{age}"
# print(text)
1.3 随机数选择
# import random
# data_list = ["iphone12","二手女友","10日游","避孕套","大保健"]
# while data_list:
# name = input("请输入自己的名字:")
# value = random.choice(data_list)
# print("恭喜{},抽中{}".format(name,value))
# data_list.remove(value)
1.4 for循环使用及乘法表
# for i in range(1, 10):
# for j in range(1, i + 1):
# text = "{}*{}".format(i, j)
# print(text, end=" ")
# print("")
1.5 文件的操作
from xml.etree import ElementTree as ET
# 创建标签
root = ET.Element("home")
# 创建节点
son1 = ET.Element('son', {
'name': '儿1'})
son2 = ET.Element('son', {
'name': '儿2'})
# 添孙子
grandson1 = ET.Element('grandson', {
'name': '儿11'})
grandson2 = ET.Element('grandson', {
'name': '儿12'})
son1.append(grandson1)
son2.append(grandson2)
# 把儿子添加到根节点中
root.append(son1)
root.append(son2)
tree = ET.ElementTree(root)
# short_empty_elements表示标签是否为短标签
tree.write('file/oo.xml', encoding='utf-8', short_empty_elements=tree)
1.6 解析xml文件
content = """<xml>
<ToUserName><![CDATA[gh_7f0B8578]]></ToUserName>
<Status><![CDATA[gh_7f0B8578]]></Status>
</xml>"""
from xml.etree import ElementTree as ET
info = {
}
root = ET.XML(content)
for node in root:
info[node.tag] = node.text
print(info)
1.7 操作Excel
# 首先下载第三方文件 pip install openpyxl
from openpyxl import load_workbook
wb = load_workbook("files/p1.xlsx")
# 循环所有sheet
# for sheet in wb:
# cell = sheet.cell(1, 1)
# print(cell.value)
# 获取第N行所有的单元格
# for cell in sheet[1]:
# print(cell.value)
# 删除列表
# del wb["用户列表"]
# wb.save('files/p2.xlsx')
# 获取某个单元格,修改值
# cell = sheet.cell(1, 1)
# cell.value = "开始"
# wb.save("p2.xlsx")
# 获取某个单元格,修改值
# sheet['B3'] = "Alex"
# wb.save("p2.xlsx")
# 获取某个单元格,