def find_details(id2find):
    surfers_f = open("surfing_data.csv")
    
    for eash_line in surfers_f:
        s = {}
        
        (s["id"], s["country"], s["average"], s["board"], s["age"]) = eash_line.split(";")
        
        if id2find == int(s["id"]):
            surfers_f.close()
            
            return(s)
        return({})
    
lookup_id = int(input("Enter the id of the surfer: "))

surfer = find_details(lookup_id)

if surfer:
    print("ID:         " + surfer["id"])
    print("Name:       " + surfer["name"])
    print("Country:    " + surfer["Average"])
    print("Board type: " + surfer["board"])
    print("Age:        " + surfer["age"])

The database file can be found at http://www.headfirstlabs.com/books/hfprog/

In the above program I'M getting two errors, one on line 7 and another on line 17. I can't figure out what's going on. It says to many values to unpack. Thanks for any and all help.

One more thing, what is a .csv file

def find_details(id2find):
    surfers_f = open("surfing_data.csv")
    
    for eash_line in surfers_f:
        s = {}
        
        (s["id"], s["name"],s["country"], s["average"], s["board"], s["age"]) = eash_line.split(";") ## name field
        
        if id2find == int(s["id"]):
            surfers_f.close()
            
            return(s)
        return({})
    
lookup_id = int(input("Enter the id of the surfer: "))

surfer = find_details(lookup_id)

if surfer:
    print("ID:         " + surfer["id"])
    print("Name:       " + surfer["name"])
    print("Country:    " + surfer["average"]) ## small a
    print("Board type: " + surfer["board"])
    print("Age:        " + surfer["age"])

csv = comma separated values file is file with separator between field (comma), typically comma,semicolon,tab,space. It has many variations: sometimes fields are quoted sometimes not etc.

Edited 6 Years Ago by pyTony: n/a

There are a couple of obvious mistakes in the original code, tony pointed some of those out already:

Looks like there are 6 items to extract in each line, s["name"] is missing

Python indentations are important, return({}) needs to line up with the for, or it will return an empty dictionary after just one iteration.

Python is case sensitive, so surfer["Average"] has to be surfer{"average"]

Note:
Comma Separated Values (CSV) files are generated by most common spreadsheet programs. In this case the comma has been replaced by a semicolon.

Edited 6 Years Ago by vegaseat: semi

Also to make your app a bit more efficient first search for the id in the file then assign them to the dictionary, some thing like this.

if str(id2find) in each_line:
   #assign values to dictionary
   #close file
   #return dict
else:
   return s

csv = comma separated values file is file with separator between field (comma), typically comma,semicolon,tab,space. It has many variations: sometimes fields are quoted sometimes not et

def find_details(id2find):
    surfers_f = open("surfing_data.csv")
    
    for eash_line in surfers_f:
        s = {}
        
        (s["id"], s["name"],s["country"], s["average"], s["board"], s["age"]) = eash_line.split(";") ## name field
        
        if id2find == int(s["id"]):
            surfers_f.close()
            
            return(s)
        return({})
    
lookup_id = int(input("Enter the id of the surfer: "))

surfer = find_details(lookup_id)

if surfer:
    print("ID:         " + surfer["id"])
    print("Name:       " + surfer["name"])
    print("Country:    " + surfer["average"]) ## small a
    print("Board type: " + surfer["board"])
    print("Age:        " + surfer["age"])

csv = comma separated values file is file with separator between field (comma), typically comma,semicolon,tab,space. It has many variations: sometimes fields are quoted sometimes not etc.

csv = comma separated values file is file with separator between field (comma), typically comma,semicolon,tab,space. It has many variations: sometimes fields are quoted sometimes not etv

I mean these lines from three different csv files, for example:

abdc,safa,dadfa,afsdaf,asdfaj,asdfads

adsfa;asfaf;dafsdaf;dfa

"asfdfja","dfasdöf","faösfa","dfasöd"

This question has already been answered. Start a new discussion instead.