Reputation: 21
I'm fairly new to Python and would like some help on properly loading separate files. My codes purpose is to open a given file, search for customers of that file by the state or state abbreviation. However, I have a separate function to open a separate file where I have (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)
By the way, my first import statement works, for whatever reason my second one doesn't.
Edit: My question is, what am I doing wrong in my state_convert
function?
Upvotes: 1
Views: 4804
Reputation: 7628
First, I suggest you to rewrite code in more pythonic way (using with
and for
statements).
This will make code easier to understand.
I suppose that problem looks like this
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
or we can rewrite
def state_convert(fileid, state):
line2 = fileid.readline()
if state in line2:
print line2
return None
else:
return None
BTW in every iteration you go deeper and deeper into file and never return to its beginning. To do this use file.seek
or file.close
or with open(..) as ..
(third is the best)
I suppose your program should look like this:
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
sorry for my English
Upvotes: 1
Reputation:
So from your code I see a few things wrong:
line2=in_file2.readline()
like Carlos Lande mentioned you should do
line2 = fileid.readline()
next I don't understand what you are trying to do with the while loop. If you are trying to print all the lines. Then your code should look like this:
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
Ok based on your comment I have modified the code. I don't know the specifics of how your file is organized (for instance does it have just the state name per line or other stuff as well).
on another note, this line is wrong:
converted_state = state_convert(in_file2, state)
state_convert doesn't return anything. It seems like you are using the state_convert function to print for you.
Upvotes: 0
Reputation: 11063
I think the problem is here:
line2=in_file2.readline()
in_file2 is not declared in that scope
try this in your state_convert definition:
line2 = fileid.readline()
Upvotes: 0