通过chardet.detect可以对编码进行检测
b'\xc0\xeb\xc0\xeb\xd4\xad\xc9\xcf\xb2\xdd\xa3\xac\xd2\xbb\xcb\xea\xd2\xbb\xbf\xdd\xc8\xd9'
{'encoding': 'GB2312', 'confidence': 0.7407407407407407, 'language': 'Chinese'}decode的作用是将其他编码的字符串转换成unicode编码,如str.decode(gbk),表示将gbk编码的字符串str转换成unicode编码。
b'\xc0\xeb\xc0\xeb\xd4\xad\xc9\xcf\xb2\xdd\xa3\xac\xd2\xbb\xcb\xea\xd2\xbb\xbf\xdd\xc8\xd9'总结:想要将其他的编码转换成UTF-8必须先将其解码成unicode然后重新编码成UTF-8,它是以unicode为转换媒介的。如:s='中文' 如果是在utf8的文件中,该字符串就是utf8编码,如果是在gb2312的文件中,则其编码为gb2312。这种情况下,要进行编码转换,都需要先用 decode方法将其转换成unicode编码,再使用encode方法将其转换成其他编码。
离离原上草,一岁一枯荣
PS:为了方便理解和记忆,可以粗略的将编码理解为加密,解码理解为解密。编码(加密)之前的文本是能看懂的明文,但是编码(加密)之后就成了看不懂的密文(bytes类型),不同的加密方式就对应不同的编码方式。只有通过对应的解码(解密)方式才能正确解密。4.3.2 字符串的创建
Traceback (most recent call last):这是因为定义字符串时,引号要两两配对,而在上面的字符串中出现了3个单引号,无法进行配对。这个时候我们可以用双引号进行定义字符串:
File "C:\Program Files\Python3102\lib\code.py", line 63, in runsource
code = self.compile(source, filename, symbol)
File "C:\Program Files\Python3102\lib\codeop.py", line 185, in call
return _maybe_compile(self.compiler, source, filename, symbol)
File "C:\Program Files\Python3102\lib\codeop.py", line 102, in _maybe_compile
raise err1
File "C:\Program Files\Python3102\lib\codeop.py", line 91, in _maybe_compile
code1 = compiler(source + "\n", filename, symbol)
File "C:\Program Files\Python3102\lib\codeop.py", line 150, in call
codeob = compile(source, filename, symbol, self.flags, True)
File "<stdin>", line 1
string = 'Let's go'
^
SyntaxError: unterminated string literal (detected at line 1)
C:\new>>> print(r'C:\new\name') # 字符串中的内容原样显示
ame
C:\some\name这样,上面的字符串中的\n才不会当成换行符显示。而是保持原样。
Traceback (most recent call last):要生成不同的字符串,应新建一个字符串:
File "C:\Program Files\Python3102\lib\code.py", line 90, in runcode
exec(code, self.locals)
File "<stdin>", line 1, in
TypeError: 'str' object does not support item assignment
'Jython'4.3.5 字符串的删除
'Pypy'
'Pyhon'4.3.6 字符串格式化
xx您好!这里是山东小红花集团...4.3.6.1 %格式化
'Hello, world'
'Hi, Yagami, you are 16 years old.'
四大天王:黎明, 华仔, 郭富城, 张学友在字符串内部,%s表示用字符串替换,%d表示用整数替换,有几个%?占位符,后面就跟几个变量或者值,顺序要对应好。如果只有一个%?,后面变量的括号可以省略。
如果你不太确定应该用什么,%s永远起作用,它会把任何数据类型转换为字符串:3.141593复制代码
- 3-000000001
'Age: 25. Gender: True'关于自定义格式,将会在后面的自定义格式章节进行详细介绍。
'growth rate: 7 %'4.3.6.2 format
'hello world'设置参数
'world hello world'
'网站名:本, 地址:www.ben.com'通过字典设置参数
'网站名:本, 地址:www.ben.com'通过列表索引设置参数
'网站名:本, 地址:www.ben.com'使用{}转义大括号
ben对应的位置是 {10}你还可以指定要转换的值是哪种类型,更准确地说,是要将其视为哪种类型。例如,你可能提供一个整数,但将其作为小数进行处理。为此可在格式说明(即冒号后面)使用字符f(表示定点数)。
'π is 3.14'关于自定义格式,将会在后面的自定义格式章节进行详细介绍。
'Hello, my name is Eric'>>> number = 7
'My lucky number is 7'>>> price = 19.99
'The price of this book is 19.99'表达式求值与函数调用
'A total number of 196'>>> f'Complex number {(2 + 2j) /(2 - 3j)}'
'Complex number(-0.15384615384615388+0.7692307692307692j)'>>> name = 'ERIC'
'My name is eric'>>> import math
'The answer is 1.1447298858494002'引号、大括号与反斜杠
'I am Eric'>>> f'I am {'Eric'}'
若 ' 和 " 不足以满足要求,还可以使用 ''' 和 """。SyntaxError: invalid syntax复制代码
- File "<stdin>", line 1
- f'I am {'Eric'}'
- ^
"He'll say I'm Eric">>> f'''He'll say {"I'm Eric"}'''
f-string大括号外如果需要显示大括号,则应输入连续两个大括号 {{ 和 }}:SyntaxError: f-string expression part cannot include a backslash复制代码
- File "<stdin>", line 1
'5 {stars}'上面提到,f-string大括号内不能使用 \ 转义,事实上不仅如此,f-string大括号内根本就不允许出现 \。如果确实需要 \,则应首先将包含 \ 的内容用一个变量表示,再在f-string大括号内填入变量名。
'{5} stars'
右对齐(数值默认对齐方式) ,&格式化使用+
^ 居中,适用于f-string和format
| python|4.3.6.2.2 数字符号相关格式描述符
| -3.1415 |
| python|
|-3.1415 |
|+3.14|4.3.6.2.3 宽度与精度相关格式描述符
|-4.13|
| 3.14|
|3.14|
| 3.1415|4.3.6.2.4 千位分隔符相关格式描述符
| 3.14|
|3.142|
| 3.141|
|31_415|4.3.6.2.5 格式类型相关格式描述符
|31,415|
Traceback (most recent call last):
File "E:\studypy\tmp.py", line 4, in
print('|%s|' % var1)
ValueError: unsupported format character '' (0x5f) at index 2
b'ab\xe7\x94\xb2\xe4\xb9\x99'4.3.7.1.2 decode(encoding="utf-8", errors="strict")
b'ab\xe7\x94\xb2\xe4\xb9\x99'4.3.7.2 查找统计类
ab甲乙
54.3.7.2.3 index、rindex
16
-1
| Python tian mao taobao |4.3.7.3.2 ljust(width[, fillchar])、rjust
|** Python tian mao taobao **|
| Python tian mao taobao |4.3.7.3.3 zfill(width)
| Python tian mao taobao ****|
|0000 Python tian mao taobao |4.3.7.4 合并截取类
abc4.3.7.4.2 lstrip()、rstrip()
|Python tian mao taobao |4.3.7.4.3 split(str="", num=string.count(str))
| Python tian mao taobao|
| Python tian mao taob|
|['Python', 'tian', 'mao', 'taobao']|4.3.7.4.4 splitlines([keepends])
|[' Pyth', 'n tian \t ma', ' \n ta', 'ba', ' ']|
|[' Python tian \t mao ', ' taobao ']|4.3.7.4.5 strip([chars])
|[' Python tian \t mao \n', ' taobao ']|
|Python tian mao taobao|4.3.7.5 判断类
|Python tian mao taob|
|True|4.3.7.5.2 islower()、isupper()
|False|
|True|4.3.7.5.3 isalnum()
|False|
|False|4.3.7.5.4 isalpha()
|False|
|True|
|False|4.3.7.5.5 isdecimal()
|False|
|False|
|True|
|False|
|False|4.3.7.5.6 isdigit()、isnumeric()
|False|
|True|
|False|
| python tian 赛车12 |4.3.7.6.2 lower()、upper()
|Python tian 赛车12 |
| python tian 赛车12 |4.3.7.6.3 replace(old, new [, max])
|PYTHON TIAN 赛车12 |
| py*Hon ian 赛车12 |4.3.7.6.4 title()
| pyHon Tian 赛车12 |
| pyTHon /an 赛车12 |
| Tian 赛车12 |4.3.7.6.6 swapcase()
| Tian 赛车12 |
| Tian 赛车12 |
{121: 97, 111: 98, 36710: None}
pathbn Tian 赛12
欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) | Powered by Discuz! X3.4 |