python function returning dictonary 1

python function returning dictonary

def query(id):
    for line in file:
        table = {}
        (table["ID"],table["name"],table["city"]) = line.split(";")
        if id == int(table["ID"]):
             file.close()
             return table
    # ID not found; close file and return empty dict
    file.close()
    return {}

Here is what the above code is Doing:
1. Open the file for reading
2. Loop through each line in the file
3. Split the line into a list of fields
4. Create a dictionary with the fields as keys
5. If the ID matches, return the dictionary
6. If the ID is not found, return an empty dictionary

Similar Posts