开发者

Converting a text file to list and then reading list to determine if an entry is within the list

I'm having a little trouble with converting a text file to a list. the text file is presented like this:

5658845
4520125
7895122
8777541
8451277
1302850
8080152

I have written code that takes user input and tries to determine if the user input is in the list. I am however having some trouble in searching the list as I can only get a result on the last result in the list, where am I going wrong?

def accountReader():

    while True:
        chargeInput = (raw_input ("Enter a charge account to be validated: "))
        if chargeInput == '':
            break
            sys.exit


        else:
            chargeAccount = open('charge_accounts.txt', 'r')
            line = chargeAccount.readline()
            while line != '':
                if chargeInput == line:
                    print chargeInput, 'was found in list.'                    
                else:
                    print chargeInput, 'not found in list.'
                    break           


   开发者_高级运维 chargeFile.close  


A line-by-line breakdown:

def accountReader():

    while True:
        chargeInput = (raw_input ("Enter a charge account to be validated: "))
        if chargeInput == '':
            break
            sys.exit

Ok, so far so good. You've created a loop that repeatedly asks the user for input and breaks when the user inputs nothing.

        else:
            chargeAccount = open('charge_accounts.txt', 'r')
            line = chargeAccount.readline()

Here's where you start running into problems. readline reads a single line from chargeAccount and stores it in line. That means you can only test one line!

            while line != '':
                if chargeInput == line:
                    print chargeInput, 'was found in list.'

This further compounds your problem. If chargeInput == line, then this prints a message and then the loop repeats. Since there's nothing to break out of the loop, this will result in an infinite loop that constantly tests one single line from the file. Also, because each line from the file ends with a newline (\n), chargeInput == line will always yield false (thanks Steven Rumbalski for reminding me of this). Use .strip() (as suggested in matchw's answer), or, if you can tolerate partial matches, you could use Python's simple substring matching functionality: if chargeInput in line.

                else:
                    print chargeInput, 'not found in list.'
                    break           


    chargeFile.close  

And here, as sarnold pointed out, you've misnamed your file; furthermore, it's in a completely different block of code, which means that you repeatedly open chargeAccount files without closing any of them.

As you can see from matchw's post, there is a much simpler way to do what you're trying to do. But I think you'd do well to figure out how to write this code correctly in the style you've chosen. I'll give you one hint: there should be a line = chargeAccount.readline() inside the innermost while loop. Do you see why? Also, you should probably exit the loop when you succeed in finding a match, not when you fail. Then you should think about a way to test whether the search was a success after the innermost loop has completed.


i would read the list like this

chargeAccount = open('charge_accounts.txt', 'r')
accts = [line.strip() for line in chareAccount]

if chareInput in accts:
  #do something
else:
  #do something else

at the very least .strip() off the readline(), your line likely looks like '5658845\n'

UPDATE

so after testing what you have with my modification it works....except it repeats indef do to the while acct != ''

here is what I changed

chargeAccount = open('charge_accounts.txt', 'r')
  accts = [line.strip() for line in chargeAccount]
  while accts != '':
     if chargeInput in accts:
        #...

I would ditch the while loop altogether, it is either in the list or its not. no need to cycle through each line.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜