如何在Python中加载文件

时间:2012-11-13 22:39:27

标签: python python-2.7

我是Python的新手,想要正确加载单独的文件。我的代码目的是打开一个给定的文件,通过州或州的缩写来搜索该文件的客户。但是,我有一个单独的函数来打开一个单独的文件,我有(name of state):(state abbreviation)

    def file_state_search(fileid, state):
        z=0
        indx = 0
        while z<25:
            line=fileid.readline()
            data_list = ("Name:", "Address:", "City:", "State:", "Zipcode:")
            line_split = line.split(":")
            if state in line:
                while indx<5:
                    print data_list[indx], line_split[indx]
                    indx = indx + 1
            elif state not in line:
                z = z + 1
    def state_convert(fileid, state):
        line2=in_file2.readline()
        while state in line2:
                print line2



    x=1
    while x==1:
        print "Choose an option:"
        print
        print "Option '1': Search Record By State"
        print
        option = raw_input("Enter an option:")
        print
        if option == "1":
            state = raw_input("Enter A State:")
            in_file = open("AdrData.txt", 'r')
            line=in_file.readline()
            print     
            in_file2 = open("States.txt", 'r')
            line2=in_file2.readline()
            converted_state = state_convert(in_file2, state)
            print converted_state
            state_find = file_state_search(in_file, state)
            print state_find
        x=raw_input("Enter '1' to continue, Enter '2' to stop: ")
        x=int(x)

顺便说一句,我的第一个导入语句无论出于何种原因都可以使用。

编辑:我的问题是,我在state_convert函数中出错了什么?

3 个答案:

答案 0 :(得分:1)

首先,我建议您以更加pythonic的方式重写代码(使用withfor语句)。 这将使代码更容易理解。

我认为问题看起来像这样

def state_convert(fileid, state):
    # here should be fileid, and not in_file2
    # you read only one line of text
    line2=in_file2.readline()
    # if state in this line it prints line, otherwise it does nothing
    while state in line2:
            print line2

或者我们可以重写

def state_convert(fileid, state):
    line2 = fileid.readline()
    if state in line2:
        print line2
        return None
    else:
        return None

BTW在每次迭代中你都会越来越深入到文件中,永远不会回到它的开头。为此,请使用file.seekfile.closewith open(..) as ..(第三是最好的)

我想你的程序应该是这样的:

def search_smth(filename,smth):
    with open(filename, 'r') as f:
        for line in f:
            if smth in line:
                # here is line with searched phrase
                data = line.split() # or anything else
                return 'anything'

 if __name__ == '__main__':
    while True:
        print '..'
        option = raw_input('..')

        if option == '..':
            with open("AdrData.txt", 'r') as f:
                header1 = f.readline()
                header2 = f.readline() # read a pair of lines
                for line in f: # iterator for every line
                    pass # do some with line content
        elif option == '..2':
            pass
        else:
            break

抱歉我的英文

答案 1 :(得分:0)

我认为问题在于:

line2=in_file2.readline()

in_file2未在该范围内声明

在你的state_convert定义中尝试这个:

line2 = fileid.readline()

答案 2 :(得分:0)

所以从你的代码中我发现了一些错误:

line2=in_file2.readline()
像卡洛斯兰德提到你应该做的

line2 = fileid.readline()

接下来我不明白你在尝试用while循环做什么。如果您正在尝试打印所有行。然后你的代码应该是这样的:

def state_convert(fileid, state):
    line2=fileid.readlines()
    for line in line2:
            lineval = line.split(":")
            if lineval[0] == state or lineval[1] == state:
               print line

根据您的评论,我修改了代码。我不知道文件的组织方式(例如,它只有每行的状态名称或其他东西)。

另一方面,这一行是错误的:

converted_state = state_convert(in_file2, state)  

state_convert不会返回任何内容。您似乎正在使用state_convert函数为您打印。