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)

ちなみに、私の最初の import ステートメントは機能しますが、何らかの理由で 2 つ目のステートメントは機能しません。

編集:私の質問は、私のstate_convert機能で何が間違っているのですか?

4

3 に答える 3

1

まず、より Pythonic な方法でコードを書き直すことをお勧めします ( withandforステートメントを使用)。これにより、コードが理解しやすくなります。

問題はこんな感じだと思います

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.seekまたはfile.closeまたはを使用しますwith open(..) as ..(3 番目が最適です)。

プログラムは次のようになるはずです。

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

カルロス・ランデがあなたがすべきだと言ったように

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 関数を使用して印刷しているようです。

于 2012-11-13T23:50:35.417 に答える
0

問題はここにあると思います:

line2=in_file2.readline()

in_file2 はそのスコープで宣言されていません

あなたのstate_convert定義でこれを試してください:

line2 = fileid.readline()
于 2012-11-13T22:53:10.657 に答える