0

For some reason, my list named lst is always empty despite myfile.txt containing numbers scattered in it. I can't figure out why. Can someone advise me? Thanks!

import re
lst = ()

fname = "myfile.txt"
try:
    with open(fname, 'r') as fh:
        for line in fh:
            lst = [int(s) for s in re.findall(r'\b\d+\b',line)]
        print lst
except IOError:
    print "Error reading file!"
2
  • well for starters, your regex doesn't appear to be looking for any numbers! Commented Feb 21, 2016 at 6:13
  • You are printing lst only after the loop... so it's really saying the last line of myfile.txt doesn't have any digits. Commented Feb 21, 2016 at 6:15

3 Answers 3

1

Right now you're not accumulating numbers through the whole file, but replacing the list every time. If you meant to get them all, something like this would be more appropriate:

import re

lst = []

fname = "myfile.txt"
try:
    with open(fname, 'r') as fh:
        for line in fh:
            lst.extend(int(s) for s in re.findall(r'\b\d+\b',line))
        print lst
except IOError:
    print "Error reading file!"
Sign up to request clarification or add additional context in comments.

Comments

1

So a couple issues, if you want to grab every digit don't use '\b\d+\b' as that will only grab digits that are not in front of or behind words. Example: it will get "23 street" -> '23' but if you want to get "23rd street" -> '23' you need to use '\d+\'. Otherwise one of the others commenting is correct, it's because your print lst is outside of the for loop. I would be willing to bet the last line of myfile.txt does not contain any digits that are by themsevles, so it never prints anything. There are a few ways to fix it, I'll show you two ways.

import re
lst = ()

fname = "myfile.txt"
try:
    with open(fname, 'r') as fh:
        for line in fh:
            lst = [int(s) for s in re.findall(r'\d+',line)]
            print lst
except IOError:
    print "Error reading file!"

This is the easier way and you deal with less code, but if you want all of the objects to live inside one array when you print it, you could do something like this too.

import re
lst = []

fname = "myfile.txt"
try:
    with open(fname, 'r') as fh:
        for line in fh:
            lst.append([int(s) for s in re.findall(r'\b\d+\b',line)])
        print lst
except IOError:
    print "Error reading file!"

You'll notice on line 2 with this one I turned it into a list instead of a tuple. This is so we can use .append on the list comprehension, and print it afterwards.

Comments

0
with open(fname, 'r') as fh:

    data = fh.read()

    lst = re.findall(r'\b\d+\b',data)

    print lst

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.