我對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
函數錯了嗎?
基於這樣的事實,你不要用「import」這個詞來顯示任何語句,我會假設你通過「導入程序」實際上是指「讀取文件的內容」。 Python具有「import」這個詞的特殊含義,但我認爲你並沒有這樣使用它。 – Gabe
@Yes Gabe這就是我的意思是抱歉。 – Alvaro
我們不能告訴你你的'state_convert'函數有什麼問題,因爲我們不知道它應該做什麼! – Gabe