python快捷键替换统一

Python016

python快捷键替换统一,第1张

查找/替换(Search/Replace) F3 下一个 Shift + F3 前一个 Ctrl + R 替换 Ctrl + Shift + F 或者连续2次敲击shift 全局查找{可以在整个项目中查找某个字符什么的,如查找某个函数名字符串看之前是怎么使用这个函数的} Ctrl + Shift + R 全局替换

刚开始学python,遇到一个字符串前面加r的问题

理论上,字符串前面加r,会消除转义字符对字符串的影响

例:

s=r'\tt'

print(s)

Output:

'\tt'

s='\tt'

print(s)

Output:

'        t'

但是我发现对\d这个转义字符是没影响的

例如

import re

def re_method():

s ='kjiabc5ty'

    print(re.search(r'abc\d',s).group())

if __name__ =='__main__':

re_method()

依然可以匹配到abc5,并输出

我百思不得其解

后来在谷歌上搜到答案,大致意思说的\d不是有效的转义序列,所以python不会更改它,所以'\d' == r'\d'是对的。由于\\  是 有效的转义序列,因此将其更改为\,因此您得到了该行为'\d' == '\\d' == r'\d'。所以,字符串有时会造成混乱。

下面我粘贴一段原话

There is a distinction you have to make between the python interpreter and the re module.

In python, a backslash followed by a character can mean a special character if the string is not rawed. For instance, \n will mean a newline character, \r will mean a carriage return, \t will mean the tab character, \b represents a nondestructive backspace. By itself, \d in a python string does not mean anything special.

In regex however, there are a bunch of characters that would otherwise not always mean anything in python. But that's the catch, 'not always'. One of the things that can be misinterpreted is \b which in python is a backspace, in regex means a word boundary. What this implies is that if you pass on an unrawed \b to the regular expression part of a regex, this \b gets substituted by the backspace  before  it is passed to the regex function and it won't mean a thing there. So you have to absolutely pass the b with its backslash and to do that, you either escape the backslash, or raw the string.

Back to your question regarding \d, \d has no special meaning whatsoever in python, so it remains untouched. The same \d passed as a regular expression gets converted by the regex engine, which is a separate entity to the python interpreter.

翻译过来

您必须在python解释器和re模块之间进行区分。

在python中,如果未原始字符串,则反斜杠后跟一个字符可以表示一个特殊字符。例如,\n表示换行符,\r表示回车,\t表示制表符,\b表示无损退格键。就其本身而言,\d在python字符串中并不表示任何特殊含义。

但是在regex中,有一堆字符在python中并不总是意味着任何东西。但这很重要,“并非总是如此”。可能被误解的一件事是\b在python中是退格,在正则表达式中是单词边界。这意味着如果您将未展开\b的正则表达式部分传递给正则表达式,则在将其传递给regex函数 之前, 它\b会被退格键所替代,并且 在 此处不会有任何意义。因此,您必须绝对传递b带有反斜杠的,然后要么转义反斜杠,要么原始字符串。

回到关于的问题\d,\d在python中没有任何特殊含义,因此保持不变。同样\d为正则表达式通过得到由正则表达式引擎,这是一个单独的实体来python解释转换。

总之,我还是理解不太清楚,但是总算知道了有这回事。

再次记录一下,

在写python程序时,常能用到一些函数和方法,总结一下,保存起来,方便查询。

一、内置函数

# abs()获取数字绝对值

# chr(i)数字转换为字符类型

# divmod() 获取两个数值的商和余数

# enumerate() 将可遍历序列组合为索引序列

# float()转换为浮点数

# format() 格式化字符串

# int()转换为整数 

# input() 接受用户输入内容

# len() 计算元素个数

# max() 返回最大值

# min() 返回最小值

# math.ceil() 返回指定数值的上舍整数

# open()打开文件并返回文件对象

# pow() 幂运算

# print()打印输出 

# range() 生成器

# reversed()反转所有元素

# round()四舍五入求值

# sorted()对可迭代对象进行排序 

# str() 转换为字符串

# sum() 求和

# set() 创建集合

# tuple() 将序列转换为元组

# zip()将可迭代对象打包成元组

二、方法

# append() 添加列表元素

# capitalize()首字母转换为大写 

# count()字符出现次数

# close() 关闭文件

# decode() 解码字符串

# dict.keys() 获取字典所有的键

# find()字符串首次出现的索引

# f.read() 读取文件内容

# dict.update()更新字典

# dict.items() 获取字典键/值对

# dict.get() 返回指定键的值

# encode() 编码字符串

# list.sort() 排序列表元素

# index() 元素首次出现的索引

# isdigit() 判断字符串是否只由数字组成

# isupper() 是否所有字母都为大写

# isnum() 判断字符串是否由字母和数字组成

# islower() 是否所有字母都为小写

# isdecimal() 检查字符串是否只包含十进制字符

# isalpha() 检测字符串是否为纯字母

# random.shuffle()随机排序

# random.sample()返回无重复随机数列表

# random.choice() 返回一个随机元素

# random.randint() 生成指定范围的随机整数

# random.randrange() 生成指定范围的指定递增基数随机整数

# pop() 删除列表中的元素

# remove()删除列表中的指定元素

# strip()去除空格

# lstrip()去除左侧空格

# rstrip() 去除右侧空格

# readline() 读取单行内容

# root.after() Tkinter中等待一段时间后再执行命令

# str.isnumeric() 验证字符串是否为数字(适用于Unicode)

# split()分割字符串

# ord() 将字符转换为整数

# replace() 字符串替换

# ljust() 左对齐填充

# rjust() 左对齐填充

# readlines() 读取所有行内容

# datetime.datetime.now() 返回指定时区的本地日期时间

# datetime.datetime.today() 获取当前本地日期的date对象

# datetime.utcnow() 返回当前UTC时间的datetime对象

# time.strptime()把时间字符串解析为元组

# time.time()返回当前时间的时间戳

# time.sleep()暂停指定秒数

# time.strftime() 返回指定格式的日期字符串

# time.mktime() 接收时间元组并返回时间戳

# os.getcwd() 获取当前工作目录

# os.listdir() 获取指定路径下的目录和文件列表

# os.makedirs() 递归创建目录

# os.rename() 重命名目录或文件

# os.path.exists() 判断路径是否存在

# upper() 全部转换为大写字母

# lower()  全部转换为小写字母

# sys.stdout.write() 标准输出打印

# sys.stdout.flush()刷新输出 

# shutil.copy() 复制单个文件到另一文件或目录

# write() 写入文件内容

# winsound.Beep() 打开电脑扬声器

# zfill() 在字符串前面填充0

三、循环语句

# break终止当前循环

# continue 终止本循环进入下一次循环

# with open() as file 以with语句打开文件(数据保存)

四、转义字符

\ 行尾续行符

\' 单引号 

\'' 双引号

\a 响铃

\e 转义

\n 换行

\t 横向制表符

\f 换页

\xyy 十六进制yy代表的字符

\\反斜杠符号

\b 退格

\000 空

\v 纵向制表符

\r 回车

\0yy 八进制yy代表的字符

\other 其他的字符以普通格式输出