Python程序教程

您现在的位置是:首页 >  Python

当前栏目

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'