2012-11-13 34 views
1

我對Python相當陌生,希望能夠正確加載單獨的文件。我的代碼的目的是打開一個給定的文件,通過狀態或狀態縮寫搜索該文件的客戶。但是,我有一個單獨的函數來打開一個單獨的文件,我有(name of state):(state abbreviation)如何在Python中加載文件

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

基於這樣的事實,你不要用「import」這個詞來顯示任何語句,我會假設你通過「導入程序」實際上是指「讀取文件的內容」。 Python具有「import」這個詞的特殊含義,但我認爲你並沒有這樣使用它。 – Gabe

+0

@Yes Gabe這就是我的意思是抱歉。 – Alvaro

+0

我們不能告訴你你的'state_convert'函數有什麼問題,因爲我們不知道它應該做什麼! – Gabe

回答

0

我認爲這個問題是在這裏:

line2=in_file2.readline() 

in_file2並不在範圍內聲明

試試這個在您的state_convert定義:

line2 = fileid.readline() 
+0

仍然沒有打印行雖然:( – Alvaro

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函數爲你打印。

+0

即時通訊嘗試讀取符合我輸入狀態的行,當我將line2 = in_file2.readline()更改爲line2 = fileid.readline()它告訴我fileid未定義 – Alvaro

+0

fileid是一個函數的參數,如果你拼寫正確,並通過它正確的文件句柄它應該工作 – redman

+0

(國家的名稱):(國家縮寫)它有這樣的組織,好吧我設法修復fileid錯誤, – Alvaro

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 

對不起我的英語