python基础知识(二)

1.元组: tuple , 特点:不可变得列表, 数据是不可被改变

  • 固定某些数据,不让外间修改
  • 元组如果只有1个元素(*),需要在元素的末尾添加一个逗号
  • 元组也有索引,查的时候就用索引
t = ("炸弹", "火箭", "坦克")
print(t)
输出: ('炸弹', '火箭', '坦克')

set() 创建空集合
tuple() 创建空元组
list() 创建空列表
str() 创建空字符串

2.set集合,set集合是无序的

lp = set()
lp.add("我")
lp.add("你")
lp.add("他")
print(lp)  # 返回的是无序的

输出:{'我', '他', '你'}

1.set()删除

lp.remove("你")
print(lp)
输出:{'我', '他'}
  1. 交集, 并集, 差集
s1 = {"王五", "刘烨", "吴睿", "吴昊"}
s2 = {"刘烨", "吴中天", "吴昊"}
# 交集
print(s1 & s2)  
print(s1.intersection(s2))
输出一样都是:{'吴昊', '刘烨'}

# 并集
print(s1 | s2) 
print(s1.union(s2))
输出一样:{'王五', '吴昊', '吴睿', '吴中天', '刘烨'}

# 差集
print(s1 - s2)  
print(s1.difference(s2))
输出一样:{'王五', '吴睿'}

3.字典 dict()

1.首先,字典是以键值对的形式进行存储数据的
2.字典的表示方式:{key:value, key2:value, key3:value}
3.而且key是单引号不是双引号,要不然会报错

1.字典的增删改查 dict()

isd = dict()
<1>添加
isd['jis'] = "我是周武王"
print(isd)
输出:{'jis': '我是周武王'}

<2>删除
lop.pop("jis") # 根据key去删除

<3>查询
kore = {'sd': "吴中天", 'su': "吴昊", 'sr': "吴中林"}
print(kore.get('sr'))
输出: 吴中林

2.字典的循环和嵌套

for key in kore:  # 字典的循环
    print(key, kore[key])
    输出:sd 吴中天
          su 吴昊
          sr 吴中林
          
<1> 希望把key单独拿出来
print(kore.keys())
输出:dict_keys(['sd', 'su', 'sr'])

<2>希望把值value单独拿出来
print(list(kore.values()))
输出:['吴中天', '吴昊', '吴中林']

<3> 直接在字典中拿到key和value
jst = {'por': "赵敏", 'music': "张无忌", 'ages': "白眉鹰王"}
for key, value in jst.items():
    print(key, value)
输出:por 赵敏
      music 张无忌
      ages 白眉隐王
      
<4> 字典删除
t = {'sor': "赵敏", 'lusic': "张无忌", 'ages': "白眉隐王", 'as': "王"}
# 删除key首字母是a开头的
po = []  # 首先要有一个空变量
for key in t:
    if key.startswith("a"):  # 当首字母为a的时候,就匹配到让后放到空变量里
        po.append(key)
for dos in po:  # 再次循环去除保存下来要删除的key,然后直接在原字典中删除
    t.pop(dos)
print(t) 

输出:{'sor': '赵敏', 'lusic': '张无忌'}         

4.字符集和编码 bytes()

encode 简称:编码 decode 简称:解码

s = "周杰伦"
bs1 = s.encode("gbk")  # encode 就将文字转为字节(简称:编码)
bs2 = s.encode("utf-8")
print(bs1)
print(bs2)
# 输出结果: b'\xd6\xdc\xbd\xdc\xc2\xd7'
#          b'\xe5\x91\xa8\xe6\x9d\xb0\xe4\xbc\xa6'
<2> 怎么把gbk的字节转化成utf-8的字节
bs = b'\xd6\xdc\xbd\xdc\xc2\xd7'
sm = bs.decode("gbk")  # decode 简称:解码
print(sm)
输出:周杰伦
bs3 = sm.encode("utf-8")  # 重新编码
print(bs3)
输出:b'\xe5\x91\xa8\xe6\x9d\xb0\xe4\xbc\xa6'

逻辑运算符:
and 并且
r 或者
not 非真即假 非假即真

5.文件操作

open(文件路径, mode= “”, encoding="") mode 的值是读 还是写 encoding是编码格式解码还是编码

openst = open(r'C:\Users\32764\Desktop\整理文件\爬取文档.txt', mode="r", encoding="utf-8")
content = openst.read()  # 全部读取文件的内容
print(content)
content.close()   # 每次操作之后要关闭连接

1.读取文件一行的 (注意open中的r 比较重要如果不加就会报错)

 lpst = open(r'C:\Users\32764\Desktop\整理文件\爬取文档.txt', mode="r", encoding="utf-8")
 keor1 = lpst.readline().strip()   # 读取文件一行内容  strip去除空格
 print(keor1)
 keor1.close() # 每次操作之后要关闭连接
 输出: 1.我是谁

2.读取每一行(注意open中的r 比较重要如果不加就会报错)

 jor = open(r'C:\Users\32764\Desktop\整理文件\爬取文档.txt', mode="r", encoding="utf-8")
 keor = jor.readlines()   # 读取文件每一行  strip去除空格
 print(keor)
 keor.close()  # 每次操作之后要关闭连接
 输出:['1.我是谁\n', '\n', '2.我在哪里\n', '\n', '3.你在干嘛呢\n', '\n', '4.我要去哪里\n', '\n', '5.你是好人吗?\n']

3.最重要的一种读取文件 (读取文件)

jpst = open(r'C:\Users\32764\Desktop\整理文件\爬取文档.txt', mode="r", encoding="utf-8")
for item in jpst:
    print(item.strip())

4.文件写入(写入文件里)

mode = w  如果文件不存在,自动的创建一个文件
open(r'C:\Users\32764\Desktop\整理文件\写入文件.txt', mode="w", encoding="utf-8")
t = open(r'C:\Users\32764\Desktop\整理文件\写入文件.txt', mode="w", encoding="utf-8")
t.write("我要吃面包")
t.flush()  # 每次操作之后要关闭连接

5.mode = "a"的时候就是在后面追加一个句话

m = open('重新文件.txt', mode="a", encoding="utf-8")
m.write("\n")
m.write("你还想吃啥?")
m.flush()

6.使用关键词with来操作,省的加末尾的flush/close了

 with open('重新文件.txt', mode="r", encoding="utf-8") as h:
     print(h.readline())  # 输出:我爱吃面食(打印一行)

7.读取图片(mode="rb"

 with open(r'C:\Users\32764\Desktop\整理文件\123.png', mode="rb") as g:
     for item in g:
         print(item.strip())

8.把图片复制到其他文件下(注意标点符号)

 with open(r"C:\Users\32764\Desktop\整理文件\123.png", mode="rb") as f1, \
         open(r"G:\python\day01\菜单菜谱.png", mode="wb") as f2:
     for item in f1:
         f2.write(item)

案例:文件修改
把文件人名单中的周改为李


import os  # 引入OS这个模块

with open(r"G:\python\day01\人名单.txt", mode="r", encoding="utf-8") as t1, \
        open(r"G:\python\day01\人名单_副本.txt", mode="w", encoding="utf-8") as t2:
    for item in t1:
        item = item.strip()  # 去掉空格
        if item.startswith("周"):  # 数据首位是“周”的匹配
            item = item.replace("周", "李")  # 替换操作(新值(李)替换旧值(周))

        t2.write(item)  # 再重新加回去
        t2.write('\n')

# 删除源文件
os.remove("人名单.txt")
# 把副本文件重命名成源文件
os.rename("人名单_副本.txt", "人名单.txt")
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

群狼之虎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值