全栈python之路——三篇文章带你踏入python大门-基础02

  1. 列表、元组操作
  2. 字符串操作
  3. 字典操作
  4. 集合操作
  5. 文件操作
  6. 字符编码与转码

一、列表、元组操作

列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储、修改等操作

定义列表
names = ['xieqc',"foll",'snake']

通过下标访问列表中的元素,下标从0开始计数

>>> names[0]
‘xieqc’
>>> names[2]
‘foll’
>>> names[-1]
‘snake’
>>> names[-2] #还可以倒着取
‘foll’

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
切片:取多个元素

 names = ["Alex","Tenglan","Eric","Rain","Tom","Amy"]
 names[1:4]  #取下标1至下标4之间的数字,包括1,不包括4
['Tenglan', 'Eric', 'Rain']
 names[1:-1] #取下标1至-1的值,不包括-1
['Tenglan', 'Eric', 'Rain', 'Tom']
 names[0:3] 
['Alex', 'Tenglan', 'Eric']
 names[:3] #如果是从头开始取,0可以忽略,跟上句效果一样
['Alex', 'Tenglan', 'Eric']
 names[3:] #如果想取最后一个,必须不能写-1,只能这么写
['Rain', 'Tom', 'Amy'] 
 names[3:-1] #这样-1就不会被包含了
['Rain', 'Tom']
 names[0::2] #后面的2是代表,每隔一个元素,就取一个
['Alex', 'Eric', 'Tom'] 
names[::2] #和上句效果一样
['Alex', 'Eric', 'Tom']

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
追加
names
['Alex', 'Tenglan', 'Eric', 'Rain', 'Tom', 'Amy']
 names.append("我是新来的")
 names
['Alex', 'Tenglan', 'Eric', 'Rain', 'Tom', 'Amy', '我是新来的']

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
插入
 names
['Alex', 'Tenglan', 'Eric', 'Rain', 'Tom', 'Amy', '我是新来的']
>>> names.insert(2,"强行从Eric前面插入")
>>> names
['Alex', 'Tenglan', '强行从Eric前面插入', 'Eric', 'Rain', 'Tom', 'Amy', '我是新来的']

>>> names.insert(5,“从eric后面插入试试新姿势”)
>>> names
[‘Alex’, ‘Tenglan’, ‘强行从Eric前面插入’, ‘Eric’, ‘Rain’, ‘从eric后面插入试试新姿势’, ‘Tom’, ‘Amy’, ‘我是新来的’]

修改
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
>>> names
['Alex', 'Tenglan', '强行从Eric前面插入', 'Eric', 'Rain', '从eric后面插入试试新姿势', 'Tom', 'Amy', '我是新来的']
>>> names[2] = "该换人了"
>>> names
['Alex', 'Tenglan', '该换人了', 'Eric', 'Rain', '从eric后面插入试试新姿势', 'Tom', 'Amy', '我是新来的']
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
删除

>>> del names[2] 
>>> names
['Alex', 'Tenglan', 'Eric', 'Rain', '从eric后面插入试试新姿势', 'Tom', 'Amy', '我是新来的']
>>> del names[4]
>>> names
['Alex', 'Tenglan', 'Eric', 'Rain', 'Tom', 'Amy', '我是新来的']
>>> 
>>> names.remove("Eric") #删除指定元素
>>> names
['Alex', 'Tenglan', 'Rain', 'Tom', 'Amy', '我是新来的']
>>> names.pop() #删除列表最后一个值 
'我是新来的'
>>> names
['Alex', 'Tenglan', 'Rain', 'Tom', 'Amy']

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
扩展

>>> names
['Alex', 'Tenglan', 'Rain', 'Tom', 'Amy']
>>> b = [1,2,3]
>>> names.extend(b)
>>> names
['Alex', 'Tenglan', 'Rain', 'Tom', 'Amy', 1, 2, 3]

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
拷贝

>>> names
['Alex', 'Tenglan', 'Rain', 'Tom', 'Amy', 1, 2, 3]

>>> name_copy = names.copy()
>>> name_copy
[‘Alex’, ‘Tenglan’, ‘Rain’, ‘Tom’, ‘Amy’, 1, 2, 3]

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
统计
>>> names
['Alex', 'Tenglan', 'Amy', 'Tom', 'Amy', 1, 2, 3]
>>> names.count("Amy")

  
  
  • 1
  • 2
  • 3
排序&翻转

>>> names
['Alex', 'Tenglan', 'Amy', 'Tom', 'Amy', 1, 2, 3]
>>> names.sort() #排序
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unorderable types: int() < str()   #3.0里不同数据类型不能放在一起排序了,擦
>>> names[-3] = '1'
>>> names[-2] = '2'
>>> names[-1] = '3'
>>> names
['Alex', 'Amy', 'Amy', 'Tenglan', 'Tom', '1', '2', '3']
>>> names.sort()
>>> names
['1', '2', '3', 'Alex', 'Amy', 'Amy', 'Tenglan', 'Tom']

>>> names.reverse() #反转
>>> names
[‘Tom’, ‘Tenglan’, ‘Amy’, ‘Amy’, ‘Alex’, ‘3’, ‘2’, ‘1’]

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
获取下标
>>> names
['Tom', 'Tenglan', 'Amy', 'Amy', 'Alex', '3', '2', '1']
>>> names.index("Amy")
2 #只返回找到的第一个下标

  
  
  • 1
  • 2
  • 3
  • 4
元组

元组其实跟列表差不多,也是存一组数,只不是它一旦创建,便不能再修改,所以又叫只读列表

语法

names = ("alex","jack","eric")

  
  
  • 1

它只有2个方法,一个是count,一个是index,完毕。

程序练习
请闭眼写出以下程序。

程序:购物车程序

需求:

扫描二维码关注公众号,回复: 10077663 查看本文章
启动程序后,让用户输入工资,然后打印商品列表
允许用户根据商品编号购买商品
用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 
可随时退出,退出时,打印已购买商品和余额
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
答案奉上。。。

#-*- coding: utf-8 -*-
#@Time    : 2019/5/26 16:36
#@Author  : Xieqc
#@Email   : [email protected]
#@File    : shopping.py
#@Software: PyCharm
#程序:购物车程序
#需求:
#启动程序后,让用户输入工资,然后打印商品列表
#允许用户根据商品编号购买商品
#用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 
#可随时退出,退出时,打印已购买商品和余额
product_list = [
    ("Mac",9000),
    ("kindke",800),
    ("tesla",900000),
    ("python book",105),
    ("bike",2000)
]
saving = input("请输入您要存入的钱数:")
shopping_car = []
if saving.isdigit():
    saving=int(saving)
while True:
    for i,v in enumerate(product_list,1):
        print(i,"&gt;&gt;&gt;&gt;&gt;",v)

    choice = input("请选择购买的商品编号【退出:q】")

    if choice.isdigit():
        choice=int(choice)

        if  choice&gt;0 and choice&lt;=len(product_list):
            p_item = product_list[choice-1]

            if p_item[1]&lt;=saving:
                saving-=p_item[1]
                shopping_car.append(p_item)
            else:
                print("亲,余额不足,还剩%s" %saving)
                print(shopping_car)
        else:
            print("编号不存在")

    elif choice=="q":
        print("--------------您已经购买以下商品----------------")
        for i in shopping_car:
            print(i)
        print("您还剩%s钱" %saving)
        break
    else:
        print("错误输入!请重新输入")

else:
print(“请输入有效的钱数!”)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60

二、字符串操作

特性:不可修改


name.capitalize()  首字母大写
name.casefold()   大写全部变小写
name.center(50,"-")  输出 '---------------------Alex Li----------------------'
name.count('lex') 统计 lex出现次数
name.encode()  将字符串编码成bytes格式
name.endswith("Li")  判断字符串是否以 Li结尾
 "Alex\tLi".expandtabs(10) 输出'Alex      Li', 将\t转换成多长的空格 
 name.find('A')  查找A,找到返回其索引, 找不到返回-1 

format :
>>> msg = “my name is {}, and age is {}”
>>> msg.format(“alex”,22)
‘my name is alex, and age is 22’
>>> msg = “my name is {1}, and age is {0}”
>>> msg.format(“alex”,22)
‘my name is 22, and age is alex’
>>> msg = “my name is {name}, and age is {age}”
>>> msg.format(age=22,name=“ale”)
‘my name is ale, and age is 22’
format_map
>>> msg.format_map({‘name’:‘alex’,‘age’:22})
‘my name is alex, and age is 22’

msg.index(‘a’) 返回a所在字符串的索引
‘9aA’.isalnum() True

‘9’.isdigit() 是否整数
name.isnumeric
name.isprintable
name.isspace
name.istitle
name.isupper
“|”.join([‘alex’,‘jack’,‘rain’])
‘alex|jack|rain’

maketrans
>>> intab = “aeiou” #This is the string having actual characters.
>>> outtab = “12345” #This is the string having corresponding mapping character
>>> trantab = str.maketrans(intab, outtab)
>>>
>>> str = “this is string example…wow!!!”
>>> str.translate(trantab)
‘th3s 3s str3ng 2x1mpl2…w4w!!!’

msg.partition(‘is’) 输出 ('my name ‘, ‘is’, ’ {name}, and age is {age}’)

>>> “alex li, chinese name is lijie”.replace(“li”,“LI”,1)
‘alex LI, chinese name is lijie’

msg.swapcase 大小写互换

>>> msg.zfill(40)
‘00000my name is {name}, and age is {age}’

>>> n4.ljust(40,"-")
‘Hello 2orld-----------------------------’
>>> n4.rjust(40,"-")
‘-----------------------------Hello 2orld’

>>> b=“ddefdsdff_哈哈”
>>> b.isidentifier() #检测一段字符串可否被当作标志符,即是否符合变量命名规则
True

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69

三、字典操作

字典一种key - value 的数据类型,使用就像我们上学用的字典,通过笔划、字母来查对应页的详细内容。

语法:
info = {
    'stu1101': "TengLan Wu",
    'stu1102': "LongZe Luola",
    'stu1103': "XiaoZe Maliya",
}

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 字典的特性:

    dict是无序的
    key必须是唯一的,so 天生去重

增加
>>> info["stu1104"] = "苍井空"
>>> info
{'stu1102': 'LongZe Luola', 'stu1104': '苍井空', 'stu1103': 'XiaoZe Maliya', 'stu1101': 'TengLan Wu'}

  
  
  • 1
  • 2
  • 3
修改
>>> info['stu1101'] = "武藤兰"
>>> info
{'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1101': '武藤兰'}

  
  
  • 1
  • 2
  • 3
删除

>>> info
{'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1101': '武藤兰'}
>>> info.pop("stu1101") #标准删除姿势
'武藤兰'
>>> info
{'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'}
>>> del info['stu1103'] #换个姿势删除
>>> info
{'stu1102': 'LongZe Luola'}
>>> 
>>> 
>>> 
>>> info = {'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'}
>>> info
{'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'} #随机删除
>>> info.popitem()
('stu1102', 'LongZe Luola')
>>> info
{'stu1103': 'XiaoZe Maliya'}

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
查找

>>> info = {'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'}
>>> 
>>> "stu1102" in info #标准用法
True
>>> info.get("stu1102")  #获取
'LongZe Luola'
>>> info["stu1102"] #同上,但是看下面
'LongZe Luola'
>>> info["stu1105"]  #如果一个key不存在,就报错,get不会,不存在只返回None
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'stu1105'

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
多级字典嵌套及操作

av_catalog = {
    "欧美":{
        "www.youporn.com": ["很多免费的,世界最大的","质量一般"],
        "www.pornhub.com": ["很多免费的,也很大","质量比yourporn高点"],
        "letmedothistoyou.com": ["多是自拍,高质量图片很多","资源不多,更新慢"],
        "x-art.com":["质量很高,真的很高","全部收费,屌比请绕过"]
    },
    "日韩":{
        "tokyo-hot":["质量怎样不清楚,个人已经不喜欢日韩范了","听说是收费的"]
    },
    "大陆":{
        "1024":["全部免费,真好,好人一生平安","服务器在国外,慢"]
    }
}

av_catalog[“大陆”][“1024”][1] += “,可以用爬虫爬下来”
print(av_catalog[“大陆”][“1024”])
#ouput
[‘全部免费,真好,好人一生平安’, ‘服务器在国外,慢,可以用爬虫爬下来’]

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
其它姿势

#values
>>> info.values()
dict_values(['LongZe Luola', 'XiaoZe Maliya'])

#keys
>>> info.keys()
dict_keys([‘stu1102’, ‘stu1103’])

#setdefault
>>> info.setdefault(“stu1106”,“Alex”)
‘Alex’
>>> info
{‘stu1102’: ‘LongZe Luola’, ‘stu1103’: ‘XiaoZe Maliya’, ‘stu1106’: ‘Alex’}
>>> info.setdefault(“stu1102”,“龙泽萝拉”)
‘LongZe Luola’
>>> info
{‘stu1102’: ‘LongZe Luola’, ‘stu1103’: ‘XiaoZe Maliya’, ‘stu1106’: ‘Alex’}

#update
>>> info
{‘stu1102’: ‘LongZe Luola’, ‘stu1103’: ‘XiaoZe Maliya’, ‘stu1106’: ‘Alex’}
>>> b = {1:2,3:4, “stu1102”:“龙泽萝拉”}
>>> info.update(b)
>>> info
{‘stu1102’: ‘龙泽萝拉’, 1: 2, 3: 4, ‘stu1103’: ‘XiaoZe Maliya’, ‘stu1106’: ‘Alex’}

#items
info.items()
dict_items([(‘stu1102’, ‘龙泽萝拉’), (1, 2), (3, 4), (‘stu1103’, ‘XiaoZe Maliya’), (‘stu1106’, ‘Alex’)])

#通过一个列表生成默认dict,有个没办法解释的坑,少用吧这个
>>> dict.fromkeys([1,2,3],‘testd’)
{1: ‘testd’, 2: ‘testd’, 3: ‘testd’}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
循环dict

#方法1
for key in info:
    print(key,info[key])

#方法2
for k,v in info.items(): #会先把dict转成list,数据里大时莫用
print(k,v)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
程序练习
程序: 三级菜单

要求:

打印省、市、县三级菜单
可返回上一级
可随时退出程序
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

menu = {
    '北京':{
        '海淀':{
            '五道口':{
                'soho':{},
                '网易':{},
                'google':{}
            },
            '中关村':{
                '爱奇艺':{},
                '汽车之家':{},
                'youku':{},
            },
            '上地':{
                '百度':{},
            },
        },
        '昌平':{
            '沙河':{
                '老男孩':{},
                '北航':{},
            },
            '天通苑':{},
            '回龙观':{},
        },
        '朝阳':{},
        '东城':{},
    },
    '上海':{
        '闵行':{
            "人民广场":{
                '炸鸡店':{}
            }
        },
        '闸北':{
            '火车战':{
                '携程':{}
            }
        },
        '浦东':{},
    },
    '山东':{},
}

exit_flag = False
current_layer = menu

layers = [menu]

while not exit_flag:
for k in current_layer:
print(k)
choice = input(">>:").strip()
if choice == “b”:
current_layer = layers[-1]
#print(“change to laster”, current_layer)
layers.pop()
elif choice not in current_layer:continue
else:
layers.append(current_layer)
current_layer = current_layer[choice]

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64

四、集合操作

集合是一个无序的,不重复的数据组合,它的主要作用如下:

  • 去重,把一个列表变成集合,就自动去重了
  • 关系测试,测试两组数据之前的交集、差集、并集等关系
常用操作
s = set([3,5,9,10])      #创建一个数值集合  

t = set(“Hello”) #创建一个唯一字符的集合

a = t | s # t 和 s的并集

b = t & s # t 和 s的交集

c = t – s # 求差集(项在t中,但不在s中)

d = t ^ s # 对称差集(项在t或s中,但不会同时出现在二者中)

基本操作:

t.add(‘x’) # 添加一项

s.update([10,37,42]) # 在s中添加多项

使用remove()可以删除一项:

t.remove(‘H’)

len(s)
set 的长度

x in s
测试 x 是否是 s 的成员

x not in s
测试 x 是否不是 s 的成员

s.issubset(t)
s <= t
测试是否 s 中的每一个元素都在 t 中

s.issuperset(t)
s >= t
测试是否 t 中的每一个元素都在 s 中

s.union(t)
s | t
返回一个新的 set 包含 s 和 t 中的每一个元素

s.intersection(t)
s & t
返回一个新的 set 包含 s 和 t 中的公共元素

s.difference(t)
s - t
返回一个新的 set 包含 s 中有但是 t 中没有的元素

s.symmetric_difference(t)
s ^ t
返回一个新的 set 包含 s 和 t 中不重复的元素

s.copy() 返回
set “s”的一个浅复制

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63

五、文件操作

对文件操作流程

  • 打开文件,得到文件句柄并赋值给一个变量
  • 通过句柄对文件进行操作
  • 关闭文件
现有文件如下
	
Somehow, it seems the love I knew was always the most destructive kind
不知为何,我经历的爱情总是最具毁灭性的的那种
Yesterday when I was young
昨日当我年少轻狂
The taste of life was sweet
生命的滋味是甜的
As rain upon my tongue
就如舌尖上的雨露
I teased at life as if it were a foolish game
我戏弄生命 视其为愚蠢的游戏
The way the evening breeze
就如夜晚的微风
May tease the candle flame
逗弄蜡烛的火苗
The thousand dreams I dreamed
我曾千万次梦见
The splendid things I planned
那些我计划的绚丽蓝图
I always built to last on weak and shifting sand
但我总是将之建筑在易逝的流沙上
I lived by night and shunned the naked light of day
我夜夜笙歌 逃避白昼赤裸的阳光
And only now I see how the time ran away
事到如今我才看清岁月是如何匆匆流逝
Yesterday when I was young
昨日当我年少轻狂
So many lovely songs were waiting to be sung
有那么多甜美的曲儿等我歌唱
So many wild pleasures lay in store for me
有那么多肆意的快乐等我享受
And so much pain my eyes refused to see
还有那么多痛苦 我的双眼却视而不见
I ran so fast that time and youth at last ran out
我飞快地奔走 最终时光与青春消逝殆尽
I never stopped to think what life was all about
我从未停下脚步去思考生命的意义
And every conversation that I can now recall
如今回想起的所有对话
Concerned itself with me and nothing else at all
除了和我相关的 什么都记不得了
The game of love I played with arrogance and pride
我用自负和傲慢玩着爱情的游戏
And every flame I lit too quickly, quickly died
所有我点燃的火焰都熄灭得太快
The friends I made all somehow seemed to slip away
所有我交的朋友似乎都不知不觉地离开了
And only now I'm left alone to end the play, yeah
只剩我一个人在台上来结束这场闹剧
Oh, yesterday when I was young
噢 昨日当我年少轻狂
So many, many songs were waiting to be sung
有那么那么多甜美的曲儿等我歌唱
So many wild pleasures lay in store for me
有那么多肆意的快乐等我享受
And so much pain my eyes refused to see
还有那么多痛苦 我的双眼却视而不见
There are so many songs in me that won't be sung
我有太多歌曲永远不会被唱起
I feel the bitter taste of tears upon my tongue
我尝到了舌尖泪水的苦涩滋味
The time has come for me to pay for yesterday
终于到了付出代价的时间 为了昨日
When I was young
当我年少轻狂

  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
基本操作
	
f = open('lyrics') #打开文件
first_line = f.readline()
print('first line:',first_line) #读一行
print('我是分隔线'.center(50,'-'))
data = f.read()# 读取剩下的所有内容,文件大时不要用
print(data) #打印文件

f.close() #关闭文件

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
打开文件的模式有:
r,只读模式(默认)。
w,只写模式。【不可读;不存在则创建;存在则删除内容;】
a,追加模式。【可读;   不存在则创建;存在则只追加内容;】

  
  
  • 1
  • 2
  • 3

“+” 表示可以同时读写某个文件

r+,可读写文件。【可读;可写;可追加】
w+,写读
a+,同a

  
  
  • 1
  • 2
  • 3

"U"表示在读取时,可以将 \r \n \r\n自动转换成 \n (与 r 或 r+ 模式同使用)

rU
r+U

  
  
  • 1
  • 2

"b"表示处理二进制文件(如:FTP发送上传ISO镜像文件,linux可忽略,windows处理二进制文件时需标注)

rb
wb
ab

  
  
  • 1
  • 2
  • 3
其它语法
    def close(self): # real signature unknown; restored from __doc__
        """
        Close the file.
    A closed file cannot be used for further I/O operations.  close() may be
    called more than once without error.
    """
    pass

def fileno(self, *args, **kwargs): # real signature unknown
    """ Return the underlying file descriptor (an integer). """
    pass

def isatty(self, *args, **kwargs): # real signature unknown
    """ True if the file is connected to a TTY device. """
    pass

def read(self, size=-1): # known case of _io.FileIO.read
    """
    注意,不一定能全读回来
    Read at most size bytes, returned as bytes.
    
    Only makes one system call, so less data may be returned than requested.
    In non-blocking mode, returns None if no data is available.
    Return an empty bytes object at EOF.
    """
    return ""

def readable(self, *args, **kwargs): # real signature unknown
    """ True if file was opened in a read mode. """
    pass

def readall(self, *args, **kwargs): # real signature unknown
    """
    Read all data from the file, returned as bytes.
    
    In non-blocking mode, returns as much as is immediately available,
    or None if no data is available.  Return an empty bytes object at EOF.
    """
    pass

def readinto(self): # real signature unknown; restored from __doc__
    """ Same as RawIOBase.readinto(). """
    pass #不要用,没人知道它是干嘛用的

def seek(self, *args, **kwargs): # real signature unknown
    """
    Move to new file position and return the file position.
    
    Argument offset is a byte count.  Optional argument whence defaults to
    SEEK_SET or 0 (offset from start of file, offset should be &gt;= 0); other values
    are SEEK_CUR or 1 (move relative to current position, positive or negative),
    and SEEK_END or 2 (move relative to end of file, usually negative, although
    many platforms allow seeking beyond the end of a file).
    
    Note that not all file objects are seekable.
    """
    pass

def seekable(self, *args, **kwargs): # real signature unknown
    """ True if file supports random-access. """
    pass

def tell(self, *args, **kwargs): # real signature unknown
    """
    Current file position.
    
    Can raise OSError for non seekable files.
    """
    pass

def truncate(self, *args, **kwargs): # real signature unknown
    """
    Truncate the file to at most size bytes and return the truncated size.
    
    Size defaults to the current file position, as returned by tell().
    The current file position is changed to the value of size.
    """
    pass

def writable(self, *args, **kwargs): # real signature unknown
    """ True if file was opened in a write mode. """
    pass

def write(self, *args, **kwargs): # real signature unknown
    """
    Write bytes b to file, return number written.
    
    Only makes one system call, so not all of the data may be written.
    The number of bytes actually written is returned.  In non-blocking mode,
    returns None if the write would block.
    """
    pass
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
with语句

为了避免打开文件后忘记关闭,可以通过管理上下文,即:

with open('log','r') as f:
...
  • 1
  • 2
  • 3

如此方式,当with代码块执行完毕时,内部会自动关闭并释放文件资源。

在Python 2.7 后,with又支持同时对多个文件的上下文进行管理,即:

with open('log1') as obj1, open('log2') as obj2:
    pass

  
  
  • 1
  • 2
程序练习

程序1: 实现简单的shell sed替换功能

程序2:修改haproxy配置文件

需求:

1、查
    输入:www.oldboy.org
    获取当前backend下的所有记录

2、新建
输入:
arg = {
‘bakend’: ‘www.oldboy.org’,
‘record’:{
‘server’: ‘100.1.7.9’,
‘weight’: 20,
‘maxconn’: 30
}
}

3、删除
输入:
arg = {
‘bakend’: ‘www.oldboy.org’,
‘record’:{
‘server’: ‘100.1.7.9’,
‘weight’: 20,
‘maxconn’: 30
}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

global       
        log 127.0.0.1 local2
        daemon
        maxconn 256
        log 127.0.0.1 local2 info
defaults
        log global
        mode http
        timeout connect 5000ms
        timeout client 50000ms
        timeout server 50000ms
        option  dontlognull

listen stats :8888
stats enable
stats uri /admin
stats auth admin:1234

frontend oldboy.org
bind 0.0.0.0:80
option httplog
option httpclose
option forwardfor
log global
acl www hdr_reg(host) -i www.oldboy.org
use_backend www.oldboy.org if www

backend www.oldboy.org
server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

六、字符编码与转码

注意:谨慎观看,硬核

什么是编码?

基本概念很简单。首先,我们从一段信息即消息说起,消息以人类可以理解、易懂的表示存在。我打算将这种表示称为“明文”(plain text)。对于说英语的人,纸张上打印的或屏幕上显示的英文单词都算作明文。

其次,我们需要能将明文表示的消息转成另外某种表示,我们还需要能将编码文本转回成明文。从明文到编码文本的转换称为“编码”,从编码文本又转回成明文则为“解码”。

编码问题是个大问题,如果不彻底解决,它就会像隐藏在丛林中的小蛇,时不时地咬你一口。
那么到底什么是编码呢?

//ASCII

记住一句话:计算机中的所有数据,不论是文字、图片、视频、还是音频文件,本质上最终都是按照类似 01010101 的二进制存储的。
再说简单点,计算机只懂二进制数字!
所以,目的明确了:如何将我们能识别的符号唯一的与一组二进制数字对应上?于是美利坚的同志想到通过一个电平的高低状态来代指0或1,
八个电平做为一组就可以表示出
256种不同状态,每种状态就唯一对应一个字符,比如A—>00010001,而英文只有26个字符,算上一些特殊字符和数字,128个状态也够
用了;每个电平称为一个比特为,约定8个比特位构成一个字节,这样计算机就可以用127个不同字节来存储英语的文字了。这就是ASCII编码。

扩展ANSI编码
刚才说了,最开始,一个字节有八位,但是最高位没用上,默认为0;后来为了计算机也可以表示拉丁文,就将最后一位也用上了,
从128到255的字符集对应拉丁文啦。至此,一个字节就用满了!

//GB2312

计算机漂洋过海来到中国后,问题来了,计算机不认识中文,当然也没法显示中文;而且一个字节所有状态都被占满了,万恶的帝国主义亡
我之心不死啊!我党也是棒,自力更生,自己重写一张表,直接生猛地将扩展的第八位对应拉丁文全部删掉,规定一个小于127的字符的意
义与原来相同,但两个大于127的字符连在一起时,就表示一个汉字,前面的一个字节(他称之为高字节)从0xA1用到0xF7,后面一个字节
(低字节)从0xA1到0xFE,这样我们就可以组合出大约7000多个简体汉字了;这种汉字方案叫做 “GB2312”。GB2312 是对 ASCII 的中文扩展。

//GBK 和 GB18030编码

但是汉字太多了,GB2312也不够用,于是规定:只要第一个字节是大于127就固定表示这是一个汉字的开始,不管后面跟的是不是扩展字符集里的
内容。结果扩展之后的编码方案被称为 GBK 标准,GBK 包括了 GB2312 的所有内容,同时又增加了近20000个新的汉字(包括繁体字)和符号。

//UNICODE编码:

很多其它国家都搞出自己的编码标准,彼此间却相互不支持。这就带来了很多问题。于是,国际标谁化组织为了统一编码:提出了标准编码准
则:UNICODE 。
UNICODE是用两个字节来表示为一个字符,它总共可以组合出65535不同的字符,这足以覆盖世界上所有符号(包括甲骨文)

  • 1
  • 2
  • 3
  • 4
  • 5

//utf8:

unicode都一统天下了,为什么还要有一个utf8的编码呢?
大家想,对于英文世界的人们来讲,一个字节完全够了,比如要存储A,本来00010001就可以了,现在吃上了unicode的大锅饭,
得用两个字节:00000000 00010001才行,浪费太严重!
基于此,美利坚的科学家们提出了天才的想法:utf8.
UTF-8(8-bit Unicode Transformation Format)是一种针对Unicode的可变长度字符编码,它可以使用1~4个字节表示一个符号,根据
不同的符号而变化字节长度,当字符在ASCII码的范围时,就用一个字节表示,所以是兼容ASCII编码的。

这样显著的好处是,虽然在我们内存中的数据都是unicode,但当数据要保存到磁盘或者用于网络传输时,直接使用unicode就远不如utf8省空间啦!
这也是为什么utf8是我们的推荐编码方式。

Unicode与utf8的关系:
一言以蔽之:Unicode是内存编码表示方案(是规范),而UTF是如何保存和传输Unicode的方案(是实现)这也是UTF与Unicode的区别。

补充:utf8是如何节约硬盘和流量的

s=“I’m”

你看到的unicode字符集是这样的编码表:

I 0049
’ 0027
m 006d

每一个字符对应一个十六进制数字。
计算机只懂二进制,因此,严格按照unicode的方式(UCS-2),应该这样存储:

I 00000000 01001001
’ 00000000 00100111
m 00000000 01101101

这个字符串总共占用了12个字节,但是对比中英文的二进制码,可以发现,英文前9位都是0!浪费啊,浪费硬盘,浪费流量。怎么办?UTF8:

I 01001001
’ 00100111
m 01101101

utf8用了10个字节,对比unicode,少了两个,因为我们的程序英文会远多于中文,所以空间会提高很多!

记住:一切都是为了节省你的硬盘和流量。

py2的string编码

在py2中,有两种字符串类型:str类型和unicode类型;注意,这仅仅是两个名字,python定义的两个名字,关键是这两种数据类型在程序运行时存在内存地址的是什么?

我们来看一下:

#coding:utf8

s1=‘苑’

print type(s1) # <type ‘str’>
print repr(s1) #’\xe8\x8b\x91

s2=u’苑’
print type(s2) # <type ‘unicode’>
print repr(s2) # u’\u82d1’

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

内置函数repr可以帮我们在这里显示存储内容。原来,str和unicode分别存的是字节数据和unicode数据;那么两种数据之间是什么关心呢?如何转换呢?这里就涉及到编码(encode)和解码(decode)了

s1=u'苑'
print repr(s1) #u'\u82d1'

b=s1.encode(‘utf8’)
print b
print type(b) #<type ‘str’>
print repr(b) #’\xe8\x8b\x91’

s2=‘启超’
u=s2.decode(‘utf8’)
print u # 启超
print type(u) # <type ‘unicode’>
print repr(u) # u’\u82d1\u660a’

#注意
u2=s2.decode(‘gbk’)
print u2 #鑻戞槉

print len(‘启超’) #6

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

无论是utf8还是gbk都只是一种编码规则,一种把unicode数据编码成字节数据的规则,所以utf8编码的字节一定要用utf8的规则解码,否则就会出现乱码或者报错的情况。

py2编码的特色:

	
#coding:utf8

print ‘启超’ # 启超
print repr(‘启超’)#’\xe8\x8b\x91\xe6\x98\x8a’

print (u"hello"+“yuan”)

#print (u’启超’+‘最帅’) #UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe6
# in position 0: ordinal not in range(128)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

Python 2 悄悄掩盖掉了 byte 到 unicode 的转换,只要数据全部是 ASCII 的话,所有的转换都是正确的,一旦一个非 ASCII 字符偷偷进入你的程序,那么默认的解码将会失效,从而造成 UnicodeDecodeError 的错误。py2编码让程序在处理 ASCII 的时候更加简单。你复出的代价就是在处理非 ASCII 的时候将会失败。

py3的string编码

python3 renamed the unicode type to str ,the old str type has been replaced by bytes.

py3也有两种数据类型:str和bytes; str类型存unicode数据,bytse类型存bytes数据,与py2比只是换了一下名字而已。


import json

s=‘启超’
print(type(s)) #<class ‘str’>
print(json.dumps(s)) # “\u82d1\u660a”

b=s.encode(‘utf8’)
print(type(b)) # <class ‘bytes’>
print(b) # b’\xe8\x8b\x91\xe6\x98\x8a’

u=b.decode(‘utf8’)
print(type(u)) #<class ‘str’>
print(u) #启超
print(json.dumps(u)) #"\u82d1\u660a"

print(len('启超’)) # 2

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
py3的编码哲学:

Python 3最重要的新特性大概要算是对文本和二进制数据作了更为清晰的区分,不再会对bytes字节串进行自动解码。文本总是Unicode,由str类型表示,二进制数据则由bytes类型表示。Python 3不会以任意隐式的方式混用str和bytes,正是这使得两者的区分特别清晰。你不能拼接字符串和字节包,也无法在字节包里搜索字符串(反之亦然),也不能将字符串传入参数为字节包的函数(反之亦然)。

	
#print('alvin'+u'yuan')#字节串和unicode连接 py2:alvinyuan
print(b'alvin'+'yuan')#字节串和unicode连接 py3:报错 can't concat bytes to str

  
  
  • 1
  • 2
  • 3

注意:无论py2,还是py3,与明文直接对应的就是unicode数据,打印unicode数据就会显示相应的明文(包括英文和中文)

文件从磁盘到内存的编码(******)

说到这,才来到我们的重点!

抛开执行执行程序,请问大家,文本编辑器大家都是用过吧,如果不懂是什么,那么word总用过吧,ok,当我们在word上编辑文字的时候,不管是中文还是英文,计算机都是不认识的,那么在保存之前数据是通过什么形式存在内存的呢?yes,就是unicode数据,为什么要存unicode数据,这是因为它的名字最屌:万国码!解释起来就是无论英文,中文,日文,拉丁文,世界上的任何字符它都有唯一编码对应,所以兼容性是最好的。

好,那当我们保存了存到磁盘上的数据又是什么呢?

答案是通过某种编码方式编码的bytes字节串。比如utf8---一种可变长编码,很好的节省了空间;当然还有历史产物的gbk编码等等。于是,在我们的文本编辑器软件都有默认的保存文件的编码方式,比如utf8,比如gbk。当我们点击保存的时候,这些编辑软件已经"默默地"帮我们做了编码工作。

那当我们再打开这个文件时,软件又默默地给我们做了解码的工作,将数据再解码成unicode,然后就可以呈现明文给用户了!所以,unicode是离用户更近的数据,bytes是离计算机更近的数据。

说了这么多,和我们程序执行有什么关系呢?

先明确一个概念:py解释器本身就是一个软件,一个类似于文本编辑器一样的软件!

现在让我们一起还原一个py文件从创建到执行的编码过程:

打开pycharm,创建hello.py文件,写入

ret=1+1
s='启超'
print(s)

  
  
  • 1
  • 2
  • 3

当我们保存的的时候,hello.py文件就以pycharm默认的编码方式保存到了磁盘;关闭文件后再打开,pycharm就再以默认的编码方式对该文件打开后读到的内容进行解码,转成unicode到内存我们就看到了我们的明文;

而如果我们点击运行按钮或者在命令行运行该文件时,py解释器这个软件就会被调用,打开文件,然后解码存在磁盘上的bytes数据成unicode数据,这个过程和编辑器是一样的,不同的是解释器会再将这些unicode数据翻译成C代码再转成二进制的数据流,最后通过控制操作系统调用cpu来执行这些二进制数据,整个过程才算结束。

那么问题来了,我们的文本编辑器有自己默认的编码解码方式,我们的解释器有吗?

当然有啦,py2默认ASCII码,py3默认的utf8,可以通过如下方式查询

	
import sys
print(sys.getdefaultencoding())

  
  
  • 1
  • 2
  • 3

大家还记得这个声明吗?

#coding:utf8

  
  
  • 1

是的,这就是因为如果py2解释器去执行一个utf8编码的文件,就会以默认地ASCII去解码utf8,一旦程序中有中文,自然就解码错误了,所以我们在文件开头位置声明 #coding:utf8,其实就是告诉解释器,你不要以默认的编码方式去解码这个文件,而是以utf8来解码。而py3的解释器因为默认utf8编码,所以就方便很多了。

注意:我们上面讲的string编码是在cpu执行程序时的存储状态,是另外一个过程,不要混淆!

常见的编码问题
  • cmd下的乱码问题
hello.py

#coding:utf8
print (‘启超’)

  • 1
  • 2
  • 3
  • 4

文件保存时的编码也为utf8。

思考:为什么在IDE下用2或3执行都没问题,在cmd.exe下3正确,2乱码呢?

我们在win下的终端即cmd.exe去执行,大家注意,cmd.exe本身也一个软件;当我们python2 hello.py时,python2解释器(默认ASCII编码)去按声明的utf8编码文件,而文件又是utf8保存的,所以没问题;问题出在当我们print’苑昊’时,解释器这边正常执行,也不会报错,只是print的内容会传递给cmd.exe用来显示,而在py2里这个内容就是utf8编码的字节数据,可这个软件默认的编码解码方式是GBK,所以cmd.exe用GBK的解码方式去解码utf8自然会乱码。

py3正确的原因是传递给cmd的是unicode数据,cmd.exe可以识别内容,所以显示没问题。

明白原理了,修改就有很多方式,比如:

	
print (u'启超')

  
  
  • 1
  • 2

改成这样后,cmd下用2也不会有问题了。

  • open()中的编码问题

创建一个hello文本,保存成utf8:

启超,你最帅!

同目录下创建一个index.py

f=open('hello')
print(f.read())

  
  
  • 1
  • 2

为什么 在linux下,结果正常:启超,在win下,乱码:鑻戞槉(py3解释器)?

因为你的win的操作系统安装时是默认的gbk编码,而linux操作系统默认的是utf8编码;

当执行open函数时,调用的是操作系统打开文件,操作系统用默认的gbk编码去解码utf8的文件,自然乱码。

解决办法:

f=open('hello',encoding='utf8')
print(f.read())

  
  
  • 1
  • 2

如果你的文件保存的是gbk编码,在win 下就不用指定encoding了。

另外,如果你的win上不需要指定给操作系统encoding=‘utf8’,那就是你安装时就是默认的utf8编码或者已经通过命令修改成了utf8编码。

注意:open这个函数在py2里和py3中是不同的,py3中有了一个encoding=None参数。

                                </div>
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-b6c3c6d139.css" rel="stylesheet">
                                            <div class="more-toolbox">
            <div class="left-toolbox">
                <ul class="toolbox-list">
                    
                    <li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#csdnc-thumbsup"></use>
                    </svg><span class="name">点赞</span>
                    <span class="count"></span>
                    </a></li>
                    <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-csdnc-Collection-G"></use>
                    </svg><span class="name">收藏</span></a></li>
                    <li class="tool-item tool-active is-share"><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;1582594662_002&quot;}"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-csdnc-fenxiang"></use>
                    </svg>分享</a></li>
                    <!--打赏开始-->
                                            <!--打赏结束-->
                                            <li class="tool-item tool-more">
                        <a>
                        <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                        </a>
                        <ul class="more-box">
                            <li class="item"><a class="article-report">文章举报</a></li>
                        </ul>
                    </li>
                                        </ul>
            </div>
                        </div>
        <div class="person-messagebox">
            <div class="left-message"><a href="https://blog.csdn.net/xie_qi_chao">
                <img src="https://profile.csdnimg.cn/B/F/6/3_xie_qi_chao" class="avatar_pic" username="xie_qi_chao">
                                        <img src="https://g.csdnimg.cn/static/user-reg-year/1x/2.png" class="user-years">
                                </a></div>
            <div class="middle-message">
                                    <div class="title"><span class="tit"><a href="https://blog.csdn.net/xie_qi_chao" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">解启超</a></span>
                                        </div>
                <div class="text"><span>发布了317 篇原创文章</span> · <span>获赞 48</span> · <span>访问量 3万+</span></div>
            </div>
                            <div class="right-message">
                                        <a href="https://im.csdn.net/im/main.html?userName=xie_qi_chao" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
                    </a>
                                                        <a class="btn btn-sm attented bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">已关注</a>
                                </div>
                        </div>
                </div>
</article>
发布了179 篇原创文章 · 获赞 180 · 访问量 7101
  1. 列表、元组操作
  2. 字符串操作
  3. 字典操作
  4. 集合操作
  5. 文件操作
  6. 字符编码与转码

一、列表、元组操作

列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储、修改等操作

定义列表
names = ['xieqc',"foll",'snake']

猜你喜欢

转载自blog.csdn.net/weixin_46575696/article/details/104959524