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