在我的邮政编码查找程序中将文本文件解析为完整的邮政编码列表时遇到问题

时间:2010-11-01 22:40:29

标签: python

大家好,感谢您查看我的问题。我想要做的是在python中编写一个“结构化”程序,它从文件中获取txt并将其解析为列表。关闭文件后,我需要在这些列表中引用用户输入(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.")

以下是zipcode txt文件中的示例:

00501,HOLTSVILLE,NY

我肯定会陷入困境,非常感谢你对此事的帮助 的修改

感谢所有人的帮助。我真的很感激。 :)

3 个答案:

答案 0 :(得分: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"

答案 1 :(得分:1)

Python的一个优点是它是交互式的。如果从processList()中取出processRecords(),然后在程序的底部放置:


if __name__ == '__main__':
 processRecords()

然后,在命令提示符下键入“python”。您将获得Python shell提示符“>>>”。在那里输入:


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 :(得分:0)

字符串没有像列表那样的append方法。我认为您要做的是将字符串zipCodecitystate附加到parsedList。这是您用来执行此操作的代码:

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

或者,更紧凑:

parsedList = [zipCode, city, state]

如果您收到其他错误消息,请告诉我,我可以提供更多建议。