使用len方法来获取字符串的长度
str="HeimaoLST"
print(len(str))
#len()查找指定内容在字符串中是否存在,若存在则返回在字符串中第一次出现的索引值否则返回-1
str="HeimaoLST"
print(str.find("H"))
#str.find(const str)判断字符串是不是以特定内容开头或者结尾
str="HeimaoLST"
print(str.startswith("H"))
print(str.endswith("H"))
#str.startswith()
#str.endswith()统计某个字符在字符串中出现的次数
str="HeimaoLST"
print(str.count("H"))
#str.count()将字符串中的a用b替换
str="HeimaoLST"
print(str.replace("H",'X'))将字符串以某字符串为界分割为列表
str="1@1@1@1"
print(str.split("@"))将字符串中的大小写转换
ex: abbcD-->ABBCD
str.upper()
str.lower()将字符串两边的空格去除
注:不可去除中间出现的空格
" b "-->"b"
str.strip()split()的逆操作
str='a'
str.join("HeimaoLST")
-->HaeaiamaaaoaLaSaT
str.join("XXX")在列表元素后面追加元素
Date = ["AAA","BBB"]
Date.append("CCC")
->["AAA","BBB","CCC"]在任意位置插入新元素
Date = ["AAA","BBB","DDD"]
Date.insert(2,"CCC")
->["AAA","BBB","CCC","DDD"]
#index为你想插入数据的那个下标
insert(index,object)将两个列表合并
类似于append追加不过操作对象是列表
Date=[1,2,3]
Date1=[4,5]
Date.extend(Date1)
Date->[1,2,3,4,5]
Name=["AAA","BBB","CCC"]
del Name[2]
Name->["AAA","BBB"]Name=["AAA","BBB","CCC"]
Name.pop()
Name->["AAA","BBB"]Name=["AAA","BBB","CCC"]
Name.remove("CCC")
Name->["AAA","BBB"]Date=[1,2,3,4]
Date[3]=5
Date->[1,2,3,5]
food_list=["胡辣汤","豆腐脑","热干面"]
food=input("请输入一种食物:")
if food in food_list:
print("在")
else:
print("不在")
# if food not in food_list 元组的大概逻辑和列表相似(可以通过下标访问对应元素 也可以打印出整个列表)
但是元组无法进行修改
date=(1,2,3,4)
date[3]=1
##无法进行上述操作切片是指对操作对象截取其中一部分的操作
切片的语法:[起始:结束:步长] 也可以简化使用[起始:结束]
注意:选取的区间从起始位开始到结束位的前一位结束即“左闭右开” 步长表示间隔
str="HeimaoLST"
str[4] #a
str[3:7] #maoL
str[1:] #eimaoLST
str[:4] #Heim
str[1:5:2] #em当访问不存在的key时会报错
peo={'name':"XX",'age':18}
peo["name"]当访问不存在的key时会返回None
peo={'name':"XX",'age':18}
peo.get("name")
python通过使用open()函数来对文件进行操作
open("文件名/文件的路径",操作模式)
f=open("test.txt",'w')//打开文件
f.write("Hello World")
f.close()//关闭文件
关键函数:write(),read();
fp=open("demo.txt",'w')//使用w模式进行写入操作时,会覆盖掉原有文件 而a模式为为在文件末尾追加内容
fp.write("im here\n")
fp=open("demo.txt",'a')
fp.write("Hello world\n"*5)
fp=open("demo.txt",'r')
print(fp.read())//read()读取文件的所有内容
print(fp.readline())//readline()逐行读取文件的内容,但只会读取一行
print(fp.readlines())//readlines()逐行读取文件的所有内容并且把每一行的内容以列表的形式保存有事我们会遇到将列表写入文件的情况 但列表位于内存中 且write只支持写入字符串
因此会存在 由内存数据-->字节数据的操作 该操作被称作"序列化" ”反序列化”同理
python中的json模块帮助我们完成这一操作
dumps /dump
import json
names=['xx','xxx','xxxx']
fp=open("test.txt",'w')
name=json.dumps(names)
fp.write(name)
fp.close()
#-----
json.dump(names,fp)
fp.close
### dump()可以看做是将转换与写入整合为了一个函数 但仍需要手动关闭文件loads/load
import json
fp=open("test.txt",'r')
content=fp.read()
fp.close()
keys=json.loads(content)
#----
keys=json.loads(fp)
fp.close()在使用文件时应该做好异常处理来进行反馈\
使用try except来进行处理
try:
fp=open()
fp.read()
except Erro:
"Try again"
import urllib.request
##下载网页
'''
url = "http://www.bilibili.com"
urllib.request.urlretrieve(url,"bilibili.html")
'''
##下载图片
'''
url_img='https://hbimg.b0.upaiyun.com/8372431cc945ac007941a4d7c4fbc2bcf1aa31bd437d5-OiQB88_fw658'
urllib.request.urlretrieve(url_img,'img.jpg')
'''
#下载视频
'''
url_video="xx"
urllib.request.urlretrieve(url_video,'video.mp4')
'''对方服务器可能会有一定的反爬虫机制 这个时候为了使用爬虫,我们需要对我们的UA进行定制
import urllib.request
url = 'https://www.baidu.com'
##定义请求头
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/110.0'
}
##定制请求对象
request = urllib.request.Request(url=url,headers=headers)
response = urllib.request.urlopen(request)
content = response.read().decode('utf-8')
print(content)quote方法来源于urllib.parse用于对文字进行转码处理
urllib.parse.quote(name)
import urllib.request
import urllib.parse
# url = "https://www.baidu.com/s?wd=卢本伟"
##中间进行了一次转码操作,但是我们人类不方便手动转码 因此转码操作很有必要
url = "https://www.baidu.com/s?wd=%E5%8D%A2%E6%9C%AC%E4%BC%9F"
name = "卢本伟"
unicode = urllib.parse.quote(name)
print(unicode)urlencode可以对多个参数进行转码操作
import urllib.request
import urllib.parse
# url = "https://www.baidu.com/s?wd=卢本伟&sex=男&loca=CN"
##中间进行了一次转码操作,但是我们人类不方便手动转码 因此转码操作很有必要
url = "https://www.baidu.com/s?wd=%E5%8D%A2%E6%9C%AC%E4%BC%9F&sex=%E7%94%B7&loca=CN"
##urlencode操作的对象是字典 使用时需要先将参数加入到字典中
Date = {
'wd':'卢本伟','sex':'男','loca':'CN'
}
New_Date = urllib.parse.urlencode(Date)
print(New_Date)以百度翻译为例
import urllib.request
import urllib.parse
url = 'https://fanyi.baidu.com/sug'
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/110.0'
}
Date = {
'kw':'super'
}
##post请求的参数必须要进行编码
##注意两次encode
Date = urllib.parse.urlencode(Date).encode('utf-8')
##post请求的内容并不能直接拼接而是以字节数据地形式发送
request = urllib.request.Request(url,Date,headers)
response = urllib.request.urlopen(request)
#不要忘了解码
content = response.read().decode('utf-8')
print(content)