1

我对 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功能中做错了什么?

4

3 回答 3

1

首先,我建议您以更 Python 的方式(使用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

顺便说一句,在每次迭代中,您都会越来越深入地进入文件,并且永远不会回到它的开头。要做到这一点,使用file.seekor file.closeor with 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

对不起我的英语不好

于 2012-11-14T01:04:20.820 回答
0

我认为问题出在这里:

line2=in_file2.readline()

in_file2 未在该范围内声明

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

line2 = fileid.readline()
于 2012-11-13T22:53:10.657 回答
0

因此,从您的代码中,我看到了一些错误:

line2=in_file2.readline()

就像卡洛斯兰德提到你应该做的那样

line2 = fileid.readline()

next 我不明白你想用 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 函数为您打印。

于 2012-11-13T23:50:35.417 回答