Nick Bohl
Nick Bohl

Reputation: 125

TypeError: findall() takes at least 2 arguments (1 given)

posted a much worse version of this question before. I've calmed down, refined my searches and I've almost figured out what I need. I'm trying to extract all the words ending in "ing" from a decently sized text file. Also, I'm supposed to be using regex but that has me incredibly confused, so at this point I'm just trying to get the results I need. here's my code:

import re

file = open('ing words.txt', 'r')
pattern = re.compile("\w+ing")

print re.findall(r'>(\w+ing<')

here's what I get:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-32-861693e3a217> in <module>()
      3 pattern = re.compile("\w+ing")
      4 
----> 5 print re.findall(r'>(\w+ing<')

TypeError: findall() takes at least 2 arguments (1 given)

I'm still very new at this, and I don't know exactly why the second argument is needed (i know that the short answer is "because", but I'd like to know the theory if someone could take the time to explain it), but more-so how to add a second argument that won't break my code even further. I'm confident (but probably wrong) that after " print re.findall(r'>(\w+ing<') " I need some way of re-telling my terminal that it needs to search within that ing words.txt.

Am I even close?

Upvotes: 0

Views: 2744

Answers (2)

Andriy Ivaneyko
Andriy Ivaneyko

Reputation: 22041

You have to pass two arguments to re.findall() or call it as pattern method:

print pattern.findall(file.read())
print re.findall(r'>(\w+ing<', file.read())

or

for line in file:
    print pattern.findall(file.read())
    print re.findall(r'>(\w+ing<', line)

Suggestion: It's better to work with file by using With statement.

with open('ing words.txt', 'r') as file:
    print pattern.findall(file.read())
    print re.findall(r'>(\w+ing<', file.read())

Upvotes: 0

alecxe
alecxe

Reputation: 473873

re.findall() requires at least 2 arguments to be provided - a pattern itself and the string to search in. You though meant to use pattern.findall() instead:

print pattern.findall(r'>(\w+ing<')

Upvotes: 4

Related Questions