Reputation: 3
I am taking an online class for python. I've been at this for 2 weeks. I've written the following code to find numbers in a sample text document. The problem I'm having is when I move from line to line and run the regex, it finds the first set of numbers, then skips any remaining numbers on the line and moves to the next line where it matches only the first number on the line. My code is below:
#!/usr/bin/python
import re
try:
fname = raw_input("Enter file name: ")
fh = open(fname)
except:
print 'Invalid Input'
quit()
numlist = list()
for line in fh:
nums = re.findall('[0-9]+',line)
if len(nums) < 1 : continue
num = int(nums[0])
numlist.append(num)
print (numlist)
Upvotes: 0
Views: 70
Reputation: 78554
As others already noted, you're discarding all other numbers in the list and taking only the first element. You can use the map
function to convert the numbers to int
and then extend the list
for line in fh:
nums = re.findall('[0-9]+',line)
if len(nums) < 1 : continue
nums = map(int, nums)
numlist.extend(nums)
Upvotes: 1
Reputation: 12558
you are explicitly telling it to skip all numbers but the first:
num = int(nums[0])
instead, use a list comprehension to coerce to int
and append the entire list using extend()
.
numlist.extend([int(x) for x in num])
Upvotes: 3
Reputation: 95968
The problem is that you're not looping on nums
, but only appending the first item in the nums
list.
To solve this, you should iterate on nums
and append each item.
Upvotes: 0