python 字符串(字符序列)和字节序列
序列,python,字符串,字符,字节
2025-03-13 21:27:17 时间
字符串(字符序列)和字节序列
字符
- 由于历史原因, 将字符定义为
unicode
字符还不够准确, 但是未来字符的定义一定是unicode
字符
字节
就是字符的二进制表现形式
码位
我们计算机显示的实际上是码位
>>> '你好'.encode("unicode_escape").decode()
'\\u4f60\\u597d'
>>>
>>> '\u4f60\u597d'
'你好'
UNICODE
标准中以4~6个十六进制数字表示
编码
字符序列(string) -> 字节序列(bytes) -------------编码(encode)
>>> "你好".encode("utf-8")
b'\xe4\xbd\xa0\xe5\xa5\xbd'
字节序列(bytes) -> 字符序列(string) -------------解码(decode)
>>> b
b'\xe4\xbd\xa0\xe5\xa5\xbd'
>>> b.decode("utf")
'你好'
编码错误
乱码和混合编码
检查编码
没有办法通过字节序列来得出编码格式, 都是统计学来预估当前的编码
# 安装chardet
pip install chardet
# 导入charet
>>> import chardet
>>> chardet.detect(b)
解决乱码和混合编码
忽略错误编码
>>> b_2.decode("utf-8", errors='ignore')
'你好'
利用鬼符来替换
>>> b_2.decode("utf-8", errors='replace')
'你好��'
字符串的CRUD操作
通过dir("")可以查看当前字符串的操作方法
Create(创建)
+
>>> a = "a"
>>> id(a)
22951584
>>> a = a + "b"
>>> id(a)
60513280
>>> a
'ab'
+=
a += "b" 就是 a = a + "b" 省略写法
Retrieve(检索)
根据索引获取字符
在计算机语言当中, 索引值是从0开始数的
>>> a = "hello, world"
>>> a[1]
'e'
find和index(获取目标字符的索引值)
>>> a.find("e")
1
>>> a.find("!")
-1
# 找不到目标字符时, index会报错
>>> a.index("!")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: substring not found
startwith和endwith
>>> f = "2020-11-22-xxxxx"
>>> f.startswith("2020-11-22")
True
>>> f = "xxxxx.jpg"
>>> f.endswith("jpg")
True
UPDATE(更新)
replace(替换)
返回的是一个新的字符串
a.replace("wer", "wor")
split(分割)
>>> a = "<<python>>, <<java>>, <<c++>>"
>>> a.split(",")
['<<python>>', ' <<java>>', ' <<c++>>']
join(拼接)
>>> b
['<<python>>', ' <<java>>', ' <<c++>>']
>>> ",".join(b)
'<<python>>, <<java>>, <<c++>>'
DELETE(删除)
strip
>>> a
' hello, world '
>>> a.strip()
'hello, world'
>>>
lstrip
rstrip
字符串的输出和输入
保存到文件
# open函数打开一个文件, 没有文件会新建, 但是路劲不对会报错
# 指定文件名, 方法(读, 写, 追加), 编码格式
output = open("output.txt", "w", encoding="utf-8")
content = "hello, world"
# 正式写入文件
output.write(content)
# 关闭文件句柄
output.close()
读取文件
input = open("output.txt", "r", encoding="utf-8")
# 获取文件中的内容
content = input.read()
print(content)
# 暂时理解为只能读取一遍
content_2 = input.read()
print(content_2)
追加文件
output = open("output.txt", "a", encoding="utf-8")
content = "\nhello, world"
# 正式写入文件
output.write(content)
# 关闭文件句柄
output.close()
字符串的格式化输出
format
按传入参数默认顺序
a = "ping"
b = "pong"
"play pingpong: {}, {}".format(a, b)
按指定参数索引
a = "ping"
b = "pong"
"play pingpong: {0}, {1}, {0}, {1}".format(a, b)
按关键词参数
a = "ping"
b = "pong"
print("play pingpong: {a}, {b}, {a}, {b}".format(a='ping', b='pong'))
按变量(推荐, 但是只有3.6以上才可以使用)
a = "ping"
b = "pong"
print(f"playing pingpong: {a}, {b}")
小数的表示
>>> "{:.2f}".format(3.14159)
'3.14'
>>>
%
>>> "playing %s %s" % ("ping", "pong")
'playing ping pong'
相关文章
- mac系统pycharm配置python环境_mac pycharm怎么设置环境
- python执行cmd命令并解析结果_python如何打包成可执行程序
- Python-drf前戏38.2-前端Vue02
- pycharm及python安装详细教程_python基础教程
- python安装dlib库_pycharm安装dlib失败
- 一对兔子从出生后第三个月起每个月_兔子繁衍问题python
- python安装numpy后pycharm导入不了_python的numpy库
- Python中的语法糖甜不甜?
- 使用Python验证并利用Redis未授权漏洞
- python和pycharm以及anaconda的安装顺序_症证病三者之间区别
- python调用matplotlib报错_pycharm没有matplotlib模块
- python中itchat_python打招呼的代码
- pycharm配置Python环境_用虚拟机在D盘创建一个虚拟环境
- 分类变量的卡方检验(python实现&SPSS实现)「建议收藏」
- pycharm安装教程2020.3.4_python安装步骤
- Python基础23-SSH模块paramiko
- 如何查看python源码_python判断路径是否存在
- Python-drf前戏38.1-前端Vue01
- python类的初始化方法_python初始化列表
- Python-基础06-文件操作