1

Currently, I have

imported csv

and made an input variable for the user to enter a registration. I also have a CSV file called

"Known file reg and names.csv"

I will refer to this as "the doc" with names,addresses and registrations; as seen here:

Bob Job GH58JBL 9, Henderson lane
Dan strobe  WK15HYO 12, Dominic Road
Alex Nail   VA11ZWU 6, Yake lane
Sam Jones   WX63GKP 273, Soomy Avenue
John English    WK64HRK 67, Wane Close

I have a program that detects whether an inputted registration in a variable called registration; matches the registration info in the doc.

import csv
registation=str(input("Please enter you registation plate."))
with open('Known file reg and names.csv', 'rt') as f:
    reader = csv.reader(f, delimiter=',')
    for row in reader:
        for field in row:
            if field == registation:
                print("We have found your reg in our database")

this works and will result in the program outputting "we have found your reg in our database" when inputting a registration that matches one in the doc.

I then have another chunk of code that is "supposed to" write both the registration variable from the program, and the matching info from the doc to another csv file with that specific persons details.

with open("saved database.csv", "w", newline="") as csvfile:
    w = csv.writer(csvfile)
    w.writerow([registation])
    w.writerow([field])

So if I inputted "WK15HYO" the program should compare the info from the doc and the inputted registration variable, and rewrite that persons info which should be

WK15HYO Dan strobe, 12, Dominic Road

However, this isn't the case and what happens instead is the program writing

67, Wane Close` and WK15HYO

. Its not writing 12, Dominic road and Dan strobe. Please can I receive some insight on what i'm doing wrong and how to solve the problem.

4
  • You should stop your iteration when you find the record. You keep going after printing that you've found the record, so field just represents the very last value of field in the file. Commented Jul 6, 2015 at 19:33
  • So how can I go about fixing this? Sorry for the confusion. Commented Jul 6, 2015 at 19:39
  • You'll need to break out of your for loops. Since you have two of them, you'll need to do something in the inner one (like setting a variable) that the outer one can check and break on as well. You might try it and post updated code, or I've no doubt someone will turn my comment into an answer soon enough. Commented Jul 6, 2015 at 19:41
  • Could you provide an example? Commented Jul 6, 2015 at 20:46

1 Answer 1

1

OK, a couple of things. First, it seems totally unnecessary for you to loop through the fields in each row. If the registration is always the second thing, you can just look for the registration at row[1]. When you wrote field to your csv file you wrote the last thing that was assigned to the variable "field", in this case the last entry in the last row of the csv, 67, Wane Close. The below code will specifically search for the registration at index 1, save the full information as user_info (or whatever name you chose), and then end your iteration.

import csv
registation=str(input("Please enter you registation plate."))
with open('Known file reg and names.csv', 'rt') as f:
    reader = csv.reader(f, delimiter=',')
    for row in reader:
        if row[1] == registration:
            user_info = row
            break

When you go to print this you now have some choices. Do you want to print all the information to one line in the order Name, Registration, Address? Then you can simply use:

with open("saved database.csv", "w", newline="") as csvfile:
    w = csv.writer(csvfile)
    w.writerow(user_info)

if you want to do something else, then you can do that easily by writing each component in the order you desire.The person's name should be user_info[0], their registration user_info[1] and their address user_info[2].

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

1 Comment

Glad it worked. If you have what you need, you should mark the question as answered.

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.