【python】常见转义字符与方法

r

r"" 的作用是去除转义字符.

即如果是“ ”那么表示一个反斜杠字符,一个字母n,而不是表示换行了。
以r开头的字符,常用于正则表达式,对应着re模块。

>>> str = 'hello
'
>>> str_r = r'hello
'
>>> print(str)
hello

>>> print(str_r)
hello

>>>

f

import time
t0 = time.time()
time.sleep(1)
name = 'processing'
# 以 f开头表示在字符串内支持大括号内的python 表达式
print(f'{name} done in {time.time() - t0:.2f} s') 
输出:
processing done in 1.00 s

b

response = b'<h1>Hello World!</h1>'     # b' ' 表示这是一个 bytes 对象

作用:b" "前缀表示:后面字符串是bytes 类型。
用处:
网络编程中,服务器和浏览器只认bytes 类型数据。
如:send 函数的参数和 recv 函数的返回值都是 bytes 类型

附:
在 Python3 中,bytes 和 str 的互相转换方式是

str.encode('utf-8')
bytes.decode('utf-8')

u

>>> str_cn = u'中文字符串'
>>> print(str_cn)
中文字符串
>>>

作用:后面字符串以 Unicode 格式 进行编码,一般用在中文字符串前面,防止因为源码储存格式问题,导致再次使用时出现乱码。

decode

decode解码,在已知字符串编码的情况下,转码为unicode。例如下面,已知str是utf-8,转成Unicode

new_str = str.decode('utf-8')

encode

在已为 unicode 的情况下,转码为其它编码。例如下面,str是Unicode,通过encode 转成utf-8

new_str = str.encode('utf-8')

某些报错及其解决方法

ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)

在解决错误之前,首先要了解unicode和utf-8的区别。
unicode指的是万国码,是一种“字码表”。而utf-8是这种字码表储存的编码方法。unicode不一定要由utf-8这种方式编成bytecode储存,也可以使用utf-16,utf-7等其他方式。目前大多都以utf-8的方式来变成bytecode。
其次,Python中字符串类型分为byte string 和 unicode string两种。
如果在python文件中指定编码方式为utf-8(#coding=utf-8),那么所有带中文的字符串都会被认为是utf-8编码的byte string(例如:mystr="你好"),但是在函数中所产生的字符串则被认为是unicode string。
问题就出在这边,unicode string 和 byte string 是不可以混合使用的,一旦混合使用了,就会产生这样的错误.

例如:

self.response.out.write("你好" + self.request.get("param"))

其中,"你好"被认为是byte string,而self.request.get("argu")的返回值被认为是unicode string。由于预设的解码器是ascii,所以就不能识别中文byte string。然后就报错了。

以下有两个解决方法:

1.将字符串全都转成byte string。

self.response.out.write("你好"+self.request.get("argu").encode("utf-8"))

2.将字符串全都转成unicode string。

self.response.out.write(u"你好"+self.request.get("argu"))

byte string转换成unicode string可以这样转unicode(unicodestring, "utf-8")

原文地址:https://www.cnblogs.com/gaoshaonian/p/15209094.html