对于正则表达式上的PythonTypeError感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍python正则\w,并为您提供关于Perl–用正则表达式上的任意操作替换正则表达式匹配、Pyth
对于正则表达式上的Python TypeError感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍python正则\w,并为您提供关于Perl – 用正则表达式上的任意操作替换正则表达式匹配、Python +正则表达式:AttributeError:'NoneType'对象没有属性'groups'、python open file, TypeError: an integer is required (got type str)、Python super()引发TypeError的有用信息。
本文目录一览:- 正则表达式上的Python TypeError(python正则\w)
- Perl – 用正则表达式上的任意操作替换正则表达式匹配
- Python +正则表达式:AttributeError:'NoneType'对象没有属性'groups'
- python open file, TypeError: an integer is required (got type str)
- Python super()引发TypeError
正则表达式上的Python TypeError(python正则\w)
所以,我有这段代码:
url = 'http://google.com'
linkregex = re.compile('<a\s*href=[\'|"](.*?)[\'"].*?>')
m = urllib.request.urlopen(url)
msg = m.read()
links = linkregex.findall(msg)
但随后python返回此错误:
links = linkregex.findall(msg)
TypeError: can't use a string pattern on a bytes-like object
我做错了什么?
Perl – 用正则表达式上的任意操作替换正则表达式匹配
s/\b(\w+)\b/ $replaces{$1} ? $replaces{$1} : $1 /g;
我想找到字符串中的所有单词,如果单词在已知单词的数组中,则替换它,否则保留它(理想情况下我想在匹配上执行任意操作,而不仅仅是三元运算符).
为此,我尝试使用三元运算符. Perl对待?和:作为文字符号,只使用变量(如果已定义)将它们连接起来.
如何让Perl处理?:在replace中作为三元运算符?
P.S:我知道我可以在下一行代码中对匹配执行操作,但为了清晰起见,我想保留一个衬里.
解决方法
s/\b(\w+)\b/ $replaces{$1} ? $replaces{$1} : $1 /ge;
Python +正则表达式:AttributeError:'NoneType'对象没有属性'groups'
我有一个要提取其子集的字符串。这是较大的Python脚本的一部分。
这是字符串:
import rehtmlString = ''</dd><dt> Fine, thank you. </dt><dd> Molt bé, gràcies. (<i>mohl behh, GRAH-syuhs</i>)''
我要拉出“ Moltbé,gràcies。mohl behh,GRAH-syuhs ”。为此,我使用正则表达式re.search
:
SearchStr = ''(\<\/dd\>\<dt\>)+ ([\w+\,\.\s]+)([\&\#\d\;]+)(\<\/dt\>\<dd\>)+ ([\w\,\s\w\s\w\?\!\.]+) (\(\<i\>)([\w\s\,\-]+)(\<\/i\>\))''Result = re.search(SearchStr, htmlString)print Result.groups()AttributeError: ''NoneType'' object has no attribute ''groups''
既然Result.groups()
不起作用,我也不想提取(即Result.group(5)
和Result.group(7)
)。但我不明白为什么会收到此错误?正则表达式可在TextWrangler中使用,为什么不能在Python中使用呢?我是Python的初学者。
答案1
小编典典import rehtmlString = ''</dd><dt> Fine, thank you. </dt><dd> Molt bé, gràcies. (<i>mohl behh, GRAH-syuhs</i>)''SearchStr = ''(\<\/dd\>\<dt\>)+ ([\w+\,\.\s]+)([\&\#\d\;]+)(\<\/dt\>\<dd\>)+ ([\w\,\s\w\s\w\?\!\.]+) (\(\<i\>)([\w\s\,\-]+)(\<\/i\>\))''Result = re.search(SearchStr.decode(''utf-8''), htmlString.decode(''utf-8''), re.I | re.U)print Result.groups()
这样工作。该表达式包含非拉丁字符,因此通常会失败。您必须解码为Unicode并使用re.U(Unicode)标志。
我也是一个初学者,我自己也遇到过几次这个问题。
python open file, TypeError: an integer is required (got type str)
file_object = open( 'ssue.csv', 'w', 'utf-8')
TypeError: an integer is required (got type str)
对于第三个参数,指定编码方式必须要加encoding=“utf-8”
read_helper=open(checkpath,"r",encoding="utf-8")
Python super()引发TypeError
在Python 2.5中,以下代码引发TypeError
:
>>> class X: def a(self): print "a">>> class Y(X): def a(self): super(Y,self).a() print "b">>> c = Y()>>> c.a()Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 3, in aTypeError: super() argument 1 must be type, not classobj
如果我更换class X
用class X(object)
,它会奏效。这有什么解释?
答案1
小编典典原因是super()
只能在新型类上运行,这在2.x系列中意味着从object
:
>>> class X(object): def a(self): print ''a''>>> class Y(X): def a(self): super(Y, self).a() print ''b''>>> c = Y()>>> c.a()ab
关于正则表达式上的Python TypeError和python正则\w的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于Perl – 用正则表达式上的任意操作替换正则表达式匹配、Python +正则表达式:AttributeError:'NoneType'对象没有属性'groups'、python open file, TypeError: an integer is required (got type str)、Python super()引发TypeError的相关信息,请在本站寻找。
本文标签: