开发者

Having trouble parsing a txt file into a list full of zip codes in my zipcode lookup program

Hello everyone thanks for looking into my problem. What I am trying to do is write a "Structured" program in python that takes txt from a file and parses it into lists. Then after closing the file, I need to reference the user input (zipcode) in those lists and then print out the city and state according to the zipcode that they entered. My instructor is having us use structure by making several functions. I know there are probably lots of more efficient ways of doing this, but I must keep the structure thats in place. EDIT Here is my code(Current):

#-----------------------------------------------------------------------
# VARIABLE DEFINITIONS

eof = False
zipRecord = ""
zipFile = ""
zipCode = []
city = []
state = []
parsedList = []

#-----------------------------------------------------------------------
# CONSTANT DEFINITIONS

USERPROMPT = "\nEnter a zip code to find (Press Enter key alone to stop): "

#-----------------------------------------------------------------------
# FUNCTION DEFINITIONS

def startUp():
    global zipFile
    print "zipcode lookup program".upper()
    zipFile = open("zipcodes.txt","r")
    loadList()

def loadList():
    while readRecord():
        pass
    processRecords()


def readRecord():
    global eof, zipList, zipCode, city, state, parsedList
    zipRecord = zipFile.readline()
    if zipRecord == "":
        eof = True
    else:
        parsedList = zipRecord.split(",")
        zipCode.append(parsedList[0])
        city.append(parsedList[1])
        state.append(parsedList[2])
        eof = False
    return not eof

def processRecords():
        userInput = raw_input(USERPROMPT)
        if userInput:
            print userInput
            print zipCode
            if userInput in zipCode:
                index_ = zipcode.index(userInput) 
                print "The city is %s and the state is %s " % \
                      (city[index_], state[index_])
            else:
                print "\nThe zip code does not exist."
        else:
      开发者_JAVA百科      print "Please enter a data"

def closeUp():
    zipFile.close()

#-----------------------------------------------------------------------
# PROGRAM'S MAIN LOGIC

startUp()
closeUp()

raw_input("\nRun complete. Press the Enter key to exit.")

Here is a sample from the zipcode txt file:

00501,HOLTSVILLE,NY

I am definitely stuck at this point and would appreciate your help in this matter.

EDIT

Thanks for all the help everyone. I really do appreciate it. :)


why you fill the lists zipcode, city , state like that, i mean in each user entry we get the next line from the file

i think that you should do :

def loadList():
    # Fill all the list first , make the readRecord() return eof (True or False).
    while readRecord():
        pass

    # than process data (check for zip code) this will run it only one time
    # but you can put it in a loop to repeat the action.
    processRecords()

about your problem :

def processRecords():
        userInput = raw_input(USERPROMPT)
        # Check if a user has entered a text or not
        if userInput:
            # check the index from zipcode   

            if userInput in zipcode:
                # the index of the zipcode in the zipcode list is the same 
                # to get related cities and states.
                index_ = zipcode.index(userInput) 
                print "The city is %s and the state is %s " % \
                      (city[index_], state[index_])
            else:
                print "\nThe zip code does not exist."
        else:
            print "Please enter a data"


one of the beauties of Python is that it's interactive. if you take processRecords() out of loadList(), and then at the bottom of your program put:


if __name__ == '__main__':
 processRecords()

Then, from the command prompt, type "python". You'll get the Python shell prompt, ">>>". There you type:


from zipcodes import * # this assumes your program is zipcodes.py
dir()  # shows you what's defined
print zipCode  # shows you what's in zipCode

that ought to help debugging.


Strings don't have an append method like lists do. What I think you're trying to do is append the strings zipCode, city, and state to parsedList. This is the code you'd use to do that:

parsedList.append(zipCode)
parsedList.append(city)
parsedList.append(state)

Or, even more compactly:

parsedList = [zipCode, city, state]

Let me know if you get another error message and I can offer more suggestions.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜