私は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
を 1 つだけ使用すれば問題match_
ありませんが、 では機能しません。それは+
奇妙です。シェルで状況をモデル化しようとしましたが、うまくいきます!!:
>>> 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']
>>>
問題がある場合は、source_list
式をテキスト ブロックと一致させて作成し、リストに配置します。これが、listed_employers() 関数の意味です。完全なコードはこちら: http://pastebin.com/bimhfAtn
誰でも私を助けることができますか?問題は何ですか?