名称错误: 未定义名称"re"...已经在代码中导入了RE并内置了函数



我一直得到"NameError:名称're'未定义",即使我已经在我的代码中导入了re,并且内置函数pat_count() library_s19_week2.py中定义的。 我尝试了所有可能的地方来导入re,但似乎都没有工作。请帮忙!

我的代码:

import re
hash_pat = re.compile(r'#w+')
hash_counter = pat_count(hash_pat)
tweet_table['hash_count'] = tweet_table.apply(lambda row: hash_counter(row['tweet']), axis=1)

错误的回溯:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-93-1880eb903ae9> in <module>()
     10 
     11 hash_pat = re.compile(r'#w+')
---> 12 hash_counter = pat_count(hash_pat)
     13 tweet_table['hash_count'] = tweet_table.apply(lambda row: hash_counter(row['tweet']), axis=1)
     14 
/content/library_s19_week2.py in pat_count(pattern)
     95 def pat_count(pattern):
     96     import re
---> 97 
     98     pat = re.compile(pattern)
     99 
NameError: name 're' is not defined

我发现了我的错误: hash_pat = re.compile(r'#w+')应该是hash_pat = r'#w+.

如回溯中的函数pat_count()所示,hash_patre.compile()的输入。

最新更新