Reputation: 1583
I want to change the contents in a list of tuples, returned by a findall() function. And I am not sure whether I could change the elements from string to integer like this. And the error always shows that I need more than 1 value.
Ntuple=[]
match = re.findall(r'AnswerCount="(\d+)"\s*CommentCount="(\d+)"', x)
print match
for tuples in match:
for posts, comments in tuples:
posts, comments = int(posts), (int(posts)+int(comments)) ## <- error
print match
Upvotes: 1
Views: 4413
Reputation: 110202
The problem is in the line for posts, comments in tuples:
. Here tuples
is actually a single tuple containing two strings, so there is no need to iterate over it. You probably want something like:
matches = re.findall(...)
for posts, comments in matches:
....
Upvotes: 2
Reputation: 500943
match
is a list of tuples. The correct way of iterating over it is:
matches = re.findall(r'AnswerCount="(\d+)"\s*CommentCount="(\d+)"', x)
for posts, comments in matches:
posts, comments = int(posts), (int(posts)+int(comments))
The conversion from string to integer is fine.
Upvotes: 1