1

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?

4
  • 3
    Based on the fact that you don't show any statements with the word import, I'm going to assume that by "import programs" you actually mean "read the contents of files". Python has a special meaning of the word import, but I don't think you're using it that way. Commented Nov 13, 2012 at 22:54
  • @Yes Gabe that is what i mean sorry. Commented Nov 13, 2012 at 23:03
  • We cannot tell you what's wrong with your state_convert function because we do not know what it's supposed to do! Commented Nov 13, 2012 at 23:12
  • read the line of a file, then print it thats all. Commented Nov 13, 2012 at 23:19

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Still not printing the line though :(
0

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.

5 Comments

im trying to read the line that matches the state I input and when i change the line2=in_file2.readline() to line2 = fileid.readline() it tells me fileid isnt defined
(name of state):(state abbreviation)it has it organized like this and okay i managed to fix the fileid error, now its not finding anything.
what does lineval mean or do?
I am trying to split the string in each line by ":" so if you had a line that looks like this: colorado:co then lineval would be a list ["colorado","co"]. So you can compare your state to each of those values.
okay however, the ultimate problem this far, is still the failure to print.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.