python出错代码函数 python出错处理( 二 )


python函数内b=b+1为什么报错python函数内b=b+1报错是因写错代码 。在Python中,所有代码都是通过正确的空格排列的 。无多出来空格或缺少空格,整个代码都不会运行,仅返回一个错误函数 。Python代码遵循PEP8空白规范,每一级缩进使用4个空格,便不会报错 。
新手python代码错误求解正则表达式中的"(?Pmatch_wordThe)"是把捕获组命名为"match_word",
所以在取匹配字符串时用pattern.search(word).group('match_word')取命名的捕获分组,
所以它不同于html语言的标签,不需要关闭标签.还有就是打印语句中的小括号不匹配.
改正后的Python程序如下(改动的地方见注释)
from math import exp,log,sqrt
import re
string = "The quick brown fox jumps over the lazy dog."
string_list = string.split()
pattern = re.compile(r"(?Pmatch_wordThe)", re.I) #这里去掉末尾的"/match_word"
print("Output #39:")
for word in string_list:
if pattern.search(word):
#这里下面语句末尾加两个右小括号
print("{:s}".format(pattern.search(word).group('match_word')))
源代码(注意源代码的缩进)
但是search函数只匹配字符串中第一次满足正则表达式pattern的内容,
如果要匹配字符串中的多个匹配对象应该用findall函数
完整的改进程序如下
import re
string = "The quick brown fox jumps over the lazy dog."
pattern = re.compile(r"The", re.I)
print(pattern.findall(string))
源代码(注意源代码的缩进)
如果非要加命名捕获组可以用finditer函数
import re
string = "The quick brown fox jumps over the lazy dog."
pattern = re.compile(r'(?Pmatch_wordThe)', re.I)
for m in pattern.finditer(string):
print(m.group('match_word'))
源代码(注意源代码的缩进)
python出错代码函数的介绍就聊到这里吧 , 感谢你花时间阅读本站内容,更多关于python出错处理、python出错代码函数的信息别忘了在本站进行查找喔 。