python - _sre.SRE_Pattern 对象在与另一个对象连接时不可调用

标签 python regex csv typeerror

我是 python 新手,我在编写正则表达式和 csv 时遇到问题。

这是代码

import re
import csv

match_text = re.compile(r'[^(<a href="/employer/\d+?\>)].+[^(\s</a>)]', re.UNICODE)
match_hh_company_url = re.compile(r'/employer/\d+', re.UNICODE)

def listing_employers():
    ...
    ## making a list with some data
    ...
    source_list = match_page.findall(data)
    source_list = set(source_list)
    return source_list

def w_to_file(f_name, source_list):
    with open(f_name, 'wb') as csvfile:
        bankwriter = csv.writer(csvfile, dialect='excel')
        for each in source_list:
            bankwriter.writerow(match_text.findall(each) + match_hh_company_url(each))

w_to_file('bank_base', listing_employers())

一切都很好,但连接 match_text.findall(each) + match_hh_company_url(each) 时出现问题 - 它会引发错误:

Traceback (most recent call last):
  File "salebase.py", line 41, in <module>
    w_to_file('bank_base', listing_employers())
  File "salebase.py", line 33, in w_to_file
    bankwriter.writerow(match_text.findall(each) + match_hh_company_url(each))
TypeError: '_sre.SRE_Pattern' object is not callable

没关系,如果我只使用一个match_,但不能使用+,那就奇怪了。 我已经尝试在 shell 中模拟情况,并且它有效!!:

>>> import re
>>> m = re.compile('\d{3}')
>>> n = re. compile('[a-z]')
>>> a = ['111 a', 'a333', 'f444b']
>>> for x in a:
...     print m.findall(x)
... 
['111']
['333']
['444']
>>> for x in a:
...     print m.findall(x) + n.findall(x)
... 
['111', 'a']
['333', 'a']
['444', 'f', 'b']
>>>

如果重要的话,我将通过匹配表达式和文本 block 制作 source_list 并将其放入列表中。这就是 listed_employers() 函数的意义。完整代码在这里:http://pastebin.com/bimhfAtn

谁能帮帮我,有什么问题吗?

最佳答案

您应该对该对象调用 findall:

match_hh_company_url.findall(each)

演示:

>>> import re
>>> r = re.compile(r'')
>>> r()
Traceback (most recent call last):
    r()
TypeError: '_sre.SRE_Pattern' object is not callable

>>> r.findall('')
['']

关于python - _sre.SRE_Pattern 对象在与另一个对象连接时不可调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19056786/

相关文章:

regex - sed 用于用 ""替换特殊字符

regex - 使用 lapply 在列表中绘制数据并使用列表元素的名称作为绘图标题

csv - 同时写入多个 csv 文件,在 Golang 中的分区列上拆分

csv - Spark : spark-csv takes too long

python - 使用文件路径的csv从azure blob容器下载数百万个文件python

Java 正则表达式匹配 Java 代码中方法调用的第一个参数

python - 在网页的 URL 中使用 Python UUID 模块生成的值是否安全?

python - python、pandas中string.contains的反转

python - SqlAlchemy:如何在 where 子句中使用选定子查询的结果

python - 如何在 Python 中获取 unicode 月份名称?