0

Write a program that outputs the first number within a file specified by the user. It should behave like:

Enter a file name: l11-1.txt
The first number is 20.

You will need to use the file object method .read(1) to read 1 character at a time, and a string object method to check if it is a number. If there is no number, the expected behaviour is:

Enter a file name: l11-2.txt
There is no number in l11-2.txt.

Why is reading 1 character at a time a better algorithm than calling .read() once and then processing the resulting string using a loop?

I have the files and it does correspond to the answers above but im not sure how to make it output properly.

The code i have so far is below:

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

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

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

so far this opens the file and reads it but the output is always no number even if there is one. Can anyone help me ?

5 Answers 5

1

OK, you've been given some instructions:

  • read a string input from the user
  • open the file given by that string
  • .read(1) a character at a time until you get the first number or EOF
  • print the number

You've got the first and second parts here (although you should use open instead of file to open a file), what next? The first thing to do is to work out your algorithm: what do you want the computer to do?

Sign up to request clarification or add additional context in comments.

3 Comments

hmmm i would want it to start reading to see if its a number and if it is print it or else print " There is no number in ", filenm
@justin: What if the file contains "Hello, 111." -- shouldn't you return 111?
@justin: well, if you start reading "Hello, 111." you will get H, then e, then l... those aren't numbers! You need to rethink your algorithm!
0

Your last line starts looping over the lines in the file, which sounds like not what your teacher wants -- they want you to read a single character. File objects have a .read() method that lets you specify how many bytes to read, so:

c = datain.read(1)

will read a single character into a string. You can then call .isdigit() on that to determine if it's a digit or not:

c.isdigit()

It sounds like you're supposed to keep reading a digit until you run out, and then concatenate them all together; if the first thing you read isn't a digit (c.isdigit() is False) you should just error out

3 Comments

it should go through the whole file and find the first instance of a number and return it.
@justin Oh, misunderstood. You just want to keep calling datain.read(1) until you get back something where .isdigit() is True
@justin Can you be more specific?
0

Your datain variable is a file object. Use its .read(1) method to read 1 character at a time. Take a look at the string methods and find one that will tell you if a string is a number.

Comments

0

Why is reading 1 character at a time a better algorithm than calling .read() once and then processing the resulting string using a loop?

Define "better".

In this case, it's "better" because it makes you think.

In some cases, it's "better" because it can save reading an entire line when reading the first few bytes is enough.

In some cases, it's "better" because the entire line may not be sitting around in the input buffer.

Comments

0

You could use regex like (searching for an integer or a float):

import re
with open(filename, 'r') as fd:
  match = re.match('([-]?\d+(\.\d+|))', fd.read())
  if match:
    print 'My first number is', match.groups()[0]

This with with anything like: "Hello 111." => will output 111.

Comments

Your Answer

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