开发者

Open file and read to find first instance of number. Python

Im stuck on a problem for an assignment, I need to write a program that opens a file on my computer, and scans that file for the first instance of a number. Once it is found it will return

The first number in , filenm is x

otherwise it will say there is no number in filenm.

My code so far is below: When i run it no matter what it always says theres no number :(

filenm = raw_input("Enter a file name: ")
datain=open(filenm,"r")

try:
    c=datain.read(1)
    result = []

    for line in datain:
        c=datain.read(1)

        while int(c) >= 0:
            c = datain.read(1)
            result.append(c)


except:
    pass

    if len(result) > 0:
            print "The first number is",(" ".join(result))+" . "
    else:
            print "There i开发者_开发技巧s no number in" , filenm + "."


That's all you need:

import re

with open("filename") as f:
    for line in f:
        s=re.search(r'\d+',line)
        if s:
            print(s.group())
            break


  1. open the file;
  2. read it in a loop char-by-char;
  3. check if the char is digit, print whatever you want;
  4. it means there are no numbers in the file, if end-of-file is reached, print "no numbers"

Use <string>.isdigit() method to check if the given string (a single character in your case) is a digit.


I don't recommend mixing iterating through a file

for line in datain:

with using the read method (or any similar one)

c=datain.read(1)

Just stick with one or the other. Personally, I would go with iterating here.


readlines() method returns a list of all the lines in the file. You can then iterate trough the list of characters in each line:

filenm = raw_input("Enter a file name: ")
datain=open(filenm,"r")

try:

    result = []

    for line in datain.readlines():
        print 'line: ' + line
        for each in line:
            try:
                # attempt casting a number to int
                int(each)
                # if it worked it add it to the result list
                result.append(each)
            except:
                pass
except:
    pass

print result
if len(result) > 0:
   print "The first number is",(" ".join(result[0]))+". "
else:
   print "There is no number in" , filenm + "."

This will only work with the first number character it finds, not sure if you actually need to extract multi digit numbers.


My thoughts:

1) As others noted, don't mask the exception. It would be better to let it be thrown - at least that way you find out what went wrong, if something went wrong.

2) You do want to read the file a line at a time, using for line in file:. The reason for this is that the numbers you want to read are basically "words" (things separated by spaces), and there isn't a built-in way to read the file a word at a time. file.read(1) reads a single byte (i.e. character, for an ASCII file), and then you have to put them together into words yourself, which is tedious. It's much easier to tell Python to split a line into words. At least, I'm assuming here that you're not supposed to pick out the "20" from "spam ham20eggs 10 lobster thermidor".

.readlines() is somewhat redundant; it's a convenience for making a list of the lines in the file - but we don't need that list; we just need the lines one at a time. There is a function defined called .xreadlines() which does that, but it's deprecated - because we can just use for line in file:. Seriously - just keep it simple.

3) int in Python will not return a negative value if the input is non-numeric. It will throw an exception. Your code does not handle that exception properly, because it would break out of the loop. There is no way to tell Python "keep going from where you threw the exception" - and there shouldn't be, because how is the rest of the code supposed to account for what happened?


Actually your code isn't too far off. There are a number of problems. One big one is that the try/except hides errors from you which might have help you figure things out yourself. Another was that you're reading the file with a mixture of a line at a time (and ignoring its contents entirely) as well as a character at a time.

There also seems to be a misunderstand on your part about what the int() function does when given a non-numeric character string, what it does is raise an exception rather than returning something less than 0. While you could enclose a call to it it in a try/except with the except being specifically for ValueError, in this case however it would be easier to just check ahead of time to see if the character is a digit since all you want to do is continue doing that until one that isn't is seen.

So here's one way your code could be revised that would address the above issues:

import string

filenm = raw_input("Enter a file name: ")
datain = open(filenm,"r")

# find first sequence of one or more digits in file
result = []
while True:
    c = datain.read(1)

    while c in string.digits:  # digit?
        result.append(c)
        c = datain.read(1)

    if c == "" or len(result) > 0:  # end-of-file or a number has been found
        break  # get out of loop

if len(result) > 0:
    print "The first number is'", "".join(result) + "'."
else:
    print "There is no number in'", filenm + "'."

close(datain)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜