Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 13891

list filtering with regex not working in jupyter?

$
0
0

These lines of code work fine in the python interpreter (ran within PyCharm)

test = ["text.txt", "text.png", "moretext.txt", "text.pdf"]p = re.compile(r'.*\.txt$')filtered= list(filter(p.match, test))

and produce the expected result:

>>> print(filtered)['text.txt', 'moretext.txt']

But they fail when entered in a Jupyter-lab cell:

import retest = ["text.txt", "text.png", "moretext.txt", "tet.pdf"]p = re.compile(r'.*\.txt$')filtered= list(filter(p.match, test))---------------------------------------------------------------------------TypeError                                 Traceback (most recent call last)Cell In[193], line 4      2 test = ["text.txt", "text.png", "moretext.txt", "tet.pdf"]      3 p = re.compile(r'.*\.txt$')----> 4 filtered= list(filter(p.match, test))TypeError: 'str' object is not callable

The error seem to indicate that p.match is read as a string and not as a function, even though it is properly recognized when entered in a cell:

p.match<function Pattern.match(string, pos=0, endpos=9223372036854775807)>

What is going wrong?


Viewing all articles
Browse latest Browse all 13891

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>