Emily Alden
Emily Alden

Reputation: 570

List not being overwritten in expected manner

I am new to Python. What my code is supposed to do is check the line against an existing list, if the line is not already in the location file it asks for the user to assign a value to it and updates the list it is validating against. I have verified that the lists are reading from the text files correctly (in the below format).

The result it is giving me is a never-ending loop because it is not finding the line in the list even after it has been added. I would expect it to break after the user enters 'n' and then some string value.

(Also there will be an additional line for if dummyvar in location:, but I haven't gotten to that yet.)

Variables

 from_location = [ company, address, city, state ] #changes line by line
 location = [[ 'company a', 'address a', 'city a', 'state a', 'classification a'],
             [ 'company b', 'address b', 'city b', 'state b', 'classification b']]
 loc_opt = ['classification a', 'classification b', 'classification c']

Code snippet:

        while from_location not in location[:][0:-1]:
                ftext=("This location has not been indexed. Please enter the " +
                      "location name or input one of these prompts \n" + 
                      " 'l' to list all acceptable locations\n" + 
                      " 'n' to add a new location\n" +
                      " 'p' to print the full line of information\n" +
                      "Company:   " +  str(from_location[0]) + " \n" +
                      "Address:   " + str(from_location[1]) + "\n" +
                      "City:   " + str(from_location[2]) + "\n" +
                      "State:   " + str(from_location[3]) + "\n")

                dummyvar = input(ftext)
                if dummyvar == "l":
                    print("Current Location Options are:  \n ")
                    for item in loc_opt:
                        print("  ", item ,"  ")
                if dummyvar == "p":
                    print("Entire Line :  ", spline,"\n")
                if dummyvar == "n":
                    new_location=input("Please input new location:\n")
                    new_location.upper()
                    new_location = [new_location]
                    loc_opt = loc_opt + new_location
                    location = location + [from_location + new_location]
                    print(location) 

Full Code

print("Press Ctrl C with the Python window selected \n"
      "at any time to stop the program. A loud beep "
      "will sound \n when the program is finished.\n\n")
date = 00000000
counter = 1
dummyvar="q"

ref_fp =(r"filepath")
loc_opt_fn = open(ref_fp + "LOCATION OPTIONS.txt")
loc_ref_fn = open(ref_fp + "LOCATION REFERENCE.txt")

loc_opt = loc_opt_fn.read().split("\n")


location = loc_ref_fn.read().split("\n")

for index in range(len(location)):
    location[index]=location[index].split("\t")

while len(str(date))!= 8 or (date < 20180101 or date > 20301231):
    date = input("What date is the file would you like to clean"
             "( yyyymmdd year like: 20190227). \n\n")
    date = str(date).strip()
    try:
        date = int(date)
    except:
        print("\nNo letters please!\n")
        date = 000

fp1 = (r"anotherfilepath")
fp2 = ( r" filename.txt")

fpr= (r"yetanotherfilepath")
filepath = fp1 + str(date) + fp2

with open(filepath) as source:
    for line in source:
        line.strip("\n")
        spline = line.split("\t")
        if counter == 1:
            spline.append("FROM")
            spline.append("TO")
        else:
            spline.append("")
            spline.append("")
        if spline[0] == "NC":
            #So we've added two columns, FROM and To
            #And we are only operating on the lines that are NC
            #Because that excludes the exceptions.
            #Now we want to check and see if the From and
            #To information is in the reference lists.
            from_location=[spline[5],spline[7],spline[9],spline[10]]
            to_location=[spline[13],spline[15],spline[17],spline[18]]

            while from_location not in location[:][0:-1]:
                    ftext=("This location has not been indexed. Please enter the " +
                          "location name or input one of these prompts \n" + 
                          " 'l' to list all acceptable locations\n" + 
                          " 'n' to add a new location\n" +
                          " 'p' to print the full line of information\n" +
                          "Company:   " +  str(from_location[0]) + " \n" +
                          "Address:   " + str(from_location[1]) + "\n" +
                          "City:   " + str(from_location[2]) + "\n" +
                          "State:   " + str(from_location[3]) + "\n")

                    dummyvar = input(ftext)
                    if dummyvar == "l":
                        print("Current Location Options are:  \n ")
                        for item in loc_opt:
                            print("  ", item ,"  ")
                    if dummyvar == "p":
                        print("Entire Line :  ", spline,"\n")
                    if dummyvar == "n":
                        new_location=input("Please input new location:\n")
                        new_location.upper()
                        new_location = [new_location]
                        loc_opt = loc_opt + new_location
                        location = location + [from_location + new_location]
                        print(location)

    counter += 1 
import winsound
winsound.Beep(600,500)
print("\n\n\n----------------------------Processing is finished.----------------------------\n\n\n") 

Upvotes: 0

Views: 37

Answers (1)

Barmar
Barmar

Reputation: 781068

location[:][0:-1]doesn't remove the last element from each element of location. It's equivalent to temp = location[:] followed by temp[0:-1]. temp is just a copy of location, and then it returns a list with all but the last element of that.

You can do what you want with a list comprehension:

while from_location not in [l[0:-1] for l in location]:

Upvotes: 1

Related Questions