在我的邮政编码查询程序中,解析txt文件为邮政编码列表时遇到问题

0 投票
3 回答
861 浏览
提问于 2025-04-16 06:24

大家好,感谢你们关注我的问题。我现在想做的是用Python写一个“结构化”的程序,这个程序需要从一个文件中读取文本,并把这些文本解析成列表。然后在关闭文件后,我需要根据用户输入的邮政编码(zipcode)在这些列表中查找,并打印出对应的城市和州。我的老师要求我们通过创建多个函数来保持结构。我知道可能有很多更有效的方法来完成这个任务,但我必须遵循现有的结构。

编辑

这是我目前的代码:

#-----------------------------------------------------------------------
# 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:
            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.")

这是邮政编码文本文件中的一个示例:

00501,HOLTSVILLE,NY

我现在确实遇到了瓶颈,非常希望能得到大家的帮助。
编辑

感谢大家的帮助,我真的很感激。:)

3 个回答

0

字符串不像列表那样有一个叫做 append 的方法。你可能想要做的是把字符串 zipCodecitystate 添加到 parsedList 里。你可以用下面的代码来实现这个:

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

或者,更简洁一点:

parsedList = [zipCode, city, state]

如果你遇到其他错误信息,告诉我,我可以给你更多建议。

1

Python的一个优点就是它可以互动使用。如果你把processRecords()这个函数从loadList()中拿出来,然后在程序的最后加上:


if __name__ == '__main__':
 processRecords()

接着,在命令行中输入“python”。你会看到Python的提示符“>>>”。在这里你可以输入:


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

这样做应该能帮助你调试程序。

2

你为什么要这样填充列表里的邮政编码、城市和州呢?我的意思是,每次用户输入时,我们都是从文件中读取下一行。

我觉得你应该这样做:

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()

关于你的问题:

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"

撰写回答