Why is this throwing an IndexError? - python

I'm trying to figure out why it is throwing that error. The length of the tuple should be 4, and it is. Any hints, ideas?
This code is for a swiss-style project I'm working on for the Intro to Programming Udacity nano degree.
Relevant code Python code from tournament.py:
def playerStandings():
"""Returns a list of the players and their win records, sorted by wins.
The first entry in the list should be the player in first place, or a player
tied for first place if there is currently a tie.
Returns:
A list of tuples, each of which contains (id, name, wins, matches):
id: the player's unique id (assigned by the database)
name: the player's full name (as registered)
wins: the number of matches the player has won
matches: the number of matches the player has played
"""
conn = connect()
c = conn.cursor()
c.execute("SELECT COUNT(id) FROM Players;")
total = c.fetchone()[0]
num_of_players = countPlayers()
standings = [None]*num_of_players
c.execute("SELECT * FROM wincounter;")
winners = c.fetchall()
i = 0
for player in winners:
standings[i] = (player[0], player[1], player[2], player[3])
i += 1
conn.close()
return standings
Relevant code from tournament_test.py:
def testStandingsBeforeMatches():
"""
Test to ensure players are properly represented in standings prior
to any matches being reported.
"""
deleteMatches()
deletePlayers()
registerPlayer("Melpomene Murray")
registerPlayer("Randy Schwartz")
standings = playerStandings()
if len(standings) < 2:
raise ValueError("Players should appear in playerStandings even before "
"they have played any matches.")
elif len(standings) > 2:
raise ValueError("Only registered players should appear in standings.")
if len(standings[0]) != 4:
raise ValueError("Each playerStandings row should have four columns.")
[(id1, name1, wins1, matches1), (id2, name2, wins2, matches2)] = standings
if matches1 != 0 or matches2 != 0 or wins1 != 0 or wins2 != 0:
raise ValueError(
"Newly registered players should have no matches or wins.")
if set([name1, name2]) != set(["Melpomene Murray", "Randy Schwartz"]):
raise ValueError("Registered players' names should appear in standings, "
"even if they have no matches played.")
print ("6. Newly registered players appear in the standings with no matches.")
PostgreSQL schema:
DROP DATABASE IF EXISTS tournament;
CREATE DATABASE tournament;
\c tournament;
CREATE TABLE players (
id serial PRIMARY KEY NOT NULL,
Name text
);
CREATE TABLE matches (
match_id serial PRIMARY KEY NOT NULL,
winner int REFERENCES players(id),
loser int REFERENCES players(id)
);
CREATE VIEW wincounter
AS
SELECT players.id,
players.name,
COUNT(matches.winner) AS wins
FROM players
LEFT JOIN matches
ON players.id = matches.winner
GROUP BY players.id;
Error message:
vagrant#vagrant-ubuntu-trusty-32:/vagrant/tournament$ python tournament_test.py
1. countPlayers() returns 0 after initial deletePlayers() execution.
2. countPlayers() returns 1 after one player is registered.
3. countPlayers() returns 2 after two players are registered.
4. countPlayers() returns zero after registered players are deleted.
5. Player records successfully deleted.
Traceback (most recent call last):
File "tournament_test.py", line 152, in <module>
testStandingsBeforeMatches()
File "tournament_test.py", line 54, in testStandingsBeforeMatches
standings = playerStandings()
File "/vagrant/tournament/tournament.py", line 85, in playerStandings
standings[i] = (player[0], player[1], player[2], player[3])
IndexError: tuple index out of range
vagrant#vagrant-ubuntu-trusty-32:/vagrant/tournament$

The error is occurring on the line:
standings[i] = (player[0], player[1], player[2], player[3])
Your player variable is a tuple representing a row from the wincounter view in your database. That view has only three columns (players.id, players.name and wins), so you get an index error when you try to access a fourth value (player[3]).
It's not entirely clear why you have that line that way at all. If you want all the items from the player tuple to be added to standings, you could do standings[i] = player, or even get rid of the explicit loop and just write standings = list(winners) instead.

Related

Update value in SQL table using SqlAlchemy in python?

"""
Created on Tue Sep 7 14:06:54 2021
#author: hp
"""
"""BOOK DETAILS IN SQL USING PYTHON----python librarian"""
import pandas as pd
import pyodbc
from sqlalchemy import create_engine,update
"""SERVER="DESKTOP-JKOITFK\SQLEXPRESS"
DATABASE="newdatabase"
DRIVER="SQL SERVER NATIVE CLIENT 11.0"
USERNAME="chitransh"
PASSWORD="reshushrey#2027"
#DATABASE_CONNECTION=f'mssql://{USERNAME}:{PASSWORD}#{SERVER}/{DATABASE}?driver={DRIVER}'"""
table_name="bookdetails"
engine=create_engine("mssql+pyodbc://#DESKTOP-JKOITFK\SQLEXPRESS/newdatabase?driver=SQL SERVER NATIVE CLIENT 11.0")
connection=engine.connect()
details={}
def bookdetails():
print("enter the book details:")
name=input("enter the name of the book:")
author=input("enter the author of the book:")
year=int(input("enter the year of the book:"))
publisher=input("enter the publisher of the book:")
quantities=int(input("enter the quantities of the book:"))
next_action=int(input("details entry completed. Press 1 for another entry, else press 2 for exit:"))
details={"BookName":[name],"Author":[author],"Year":[year],"Publisher":[publisher],"Quantities":[quantities]}
dict_df=pd.DataFrame(details)
#print(dict_df)
create_table=dict_df.to_sql(table_name,connection,if_exists="append",index=False)
if next_action==1:
bookdetails()
else:
authorized()
def issuebooks():
print("issue the books")
issue_book=input("which book to issue:")
frame=pd.read_sql("select Quantities from bookdetails where BookName = '{}'".format(issue_book),connection)
updated_value_query=(update(bookdetails).values(Quantities=(int(frame.values)-1)).where(bookdetails.BookName=='{}'.format(issue_book)))
connection.execute(updated_value_query)
def depositbooks():
print("deposit the books")
def authorized():
action=int(input("enter 1 for entering book details, enter 2 to issue books, enter 3 to deposit the book, enter 4 for exit:"))
if action==1:
bookdetails()
elif action==2:
issuebooks()
elif action==3:
depositbooks()
#else:
# main()
def enter_func(username,password):
if username not in librarian.keys():
print("you are not authorized to enter")
else:
if password==librarian[username]:
print("enter")
authorized()
else:
print("password donot match,try again")
#main()
while True:
first=int(input("press 1 to login, 2 for exit:"))
if (first==1):
librarian={"deepika":"chiku","pragya":"praveen"}
username=input("enter the username:")
password=input("enter the password:")
enter_func(username,password)
else:
break
I am try to make a book entry system and for that i am trying to connect SQL and python. When ever i try to update a value in SQL using update query, it shows the error
press 1 to login, 2 for exit:1
enter the username:deepika
enter the password:chiku
enter
enter 1 for entering book details, enter 2 to issue books, enter 3 to deposit the book, enter 4 for exit:2
issue the books
which book to issue:shiva2
Traceback (most recent call last):
File "<ipython-input-1-df831d64649f>", line 1, in <module>
runfile('C:/Users/hp/Desktop/project_part1.py', wdir='C:/Users/hp/Desktop')
File "C:\Users\hp\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
execfile(filename, namespace)
File "C:\Users\hp\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/hp/Desktop/project_part1.py", line 101, in <module>
enter_func(username,password)
File "C:/Users/hp/Desktop/project_part1.py", line 89, in enter_func
authorized()
File "C:/Users/hp/Desktop/project_part1.py", line 77, in authorized
issuebooks()
File "C:/Users/hp/Desktop/project_part1.py", line 61, in issuebooks
updated_value_query=update(bookdetails).values(Quantities=frame-1).where("BookName=='{}'".format(issue_book))
File "<string>", line 2, in update
File "C:\Users\hp\Anaconda3\lib\site-packages\sqlalchemy\sql\dml.py", line 735, in __init__
ValuesBase.__init__(self, table, values, prefixes)
File "C:\Users\hp\Anaconda3\lib\site-packages\sqlalchemy\sql\dml.py", line 201, in __init__
self.table = _interpret_as_from(table)
File "C:\Users\hp\Anaconda3\lib\site-packages\sqlalchemy\sql\selectable.py", line 49, in _interpret_as_from
raise exc.ArgumentError("FROM expression expected")
ArgumentError: FROM expression expected
This is the error which i am facing. It is saying FROM expression is expected, but when i write query in SQL, no FROM expression is written. I want to update the subtracted value in the SQL table.
I couldn't reproduce your exact error but this line
updated_value_query=(update(bookdetails).values(Quantities=(int(frame.values)-1)).where(bookdetails.BookName=='{}'.format(issue_book)))
needs a couple of changes to work:
the name bookdetails refers to the function bookdetails, so it isn't a valid argument for update, which expects a Table object or similar
in the where clause, the Quantities attribute must accessed through a table's columns or c attribute
import sqlalchemy as sa
...
# Create a table object that maps to the table in the database
bookdetails_table = sa.Table(table_name, sa.MetaData(), autoload_with=engine)
# Use the table object in the query
updated_value_query = (
update(bookdetails_table)
.values(Quantities=(int(frame.values) - 1))
.where(bookdetails_table.c.BookName == '{}'.format(issue_book))
)
You don't really need Pandas for your code, you could replace it with SQLAlchemy Core inserts and updates. For example (assuming SQLAlchemy 1.4+)
from sqlalchemy import create_engine, update
import sqlalchemy as sa
table_name = 'bookdetails'
engine = create_engine(...)
# Create a table and assign it to a global variable.
# Lowercase table and column names cause fewer problems than mixed or upper case
metadata = sa.MetaData()
book_table = sa.Table(
table_name,
metadata,
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('bookname', sa.String(128)),
sa.Column('author', sa.String(128)),
sa.Column('year', sa.Integer),
sa.Column('publisher', sa.String(128)),
sa.Column('quantities', sa.Integer),
)
book_table.create(engine, checkfirst=True)
def bookdetails():
print('enter the book details:')
name = input('enter the name of the book:')
author = input('enter the author of the book:')
year = int(input('enter the year of the book:'))
publisher = input('enter the publisher of the book:')
quantities = int(input('enter the quantities of the book:'))
next_action = int(
input(
'details entry completed. Press 1 for another entry, else press 2 for exit:'
)
)
details = {
'bookname': name,
'author': author,
'year': year,
'publisher': publisher,
'quantities': quantities,
}
insert = book_table.insert().values(**details)
with engine.begin() as conn:
conn.execute(insert)
if next_action == 1:
bookdetails()
else:
authorized()
def issuebooks():
print('issue the books')
issue_book = input('which book to issue:')
# Here we don't need to select and then update: we can express the update
# as an operation on the column.
updated_value_query = (
update(book_table)
.values(quantities=book_table.c.quantities - 1)
.where(book_table.c.bookname == issue_book)
)
with engine.begin() as conn:
conn.execute(updated_value_query)

Save a query result on a variable

I´m trying to save a SQLAlchemy query result on a python variable (in case there is one result) in order to insert it with other data later. Is there any chance to do something like that?
Things you may want to know:
I'm trying to connect to an Oracle DB
I'm using python 3.7.1
Till now I´ve been able to do this:**
try:
map_projn_id_search = Session.execute("""
SELECT GEOFRAME_MAP_PROJN_ID
FROM V_GEOFRAME_MAP_PROJN
WHERE COORD_REF_SYS_CODE = :crs AND TRNSFRM = :tfm"""
, {"crs" : crs , "tfm" : tfm} )
map_projn_id_search.fetchone()
except Exception as e:
print("error")
else:
#If it doesn´t exist I need to insert it into the V_GEOFRAME_MAP_PROJN
if map_projn_id_search.rowcount == 0:
instances_with_unkown = ['SAMSDB', 'CASSDB', 'CCUSDB', 'EMASDB', 'EMLSDB', 'HNOSDB', 'KULSDB', 'NAFSDB', 'RUSSDB', 'YUZSDB', 'AMESDB', 'LAGSDB']
instances_with_UNKNWON =[ 'USASDB']
if emsdb_instance in instances_with_unkown:
projn_id = "Unknown"
print(f'\n The EPSG code (ProjCRS:GeogCRS:Tfm) does not exist on EMSDB. Set {projn_id} as CRS. \n')
elif emsdb_instance in instances_with_UNKNWON:
projn_id = "UNKNOWN"
print(f'\n The EPSG code (ProjCRS:GeogCRS:Tfm) does not exist on EMSDB. Set {projn_id} as CRS. \n')
else:
#If it exists then print that it exists so the user knows
print("\n Cannot insert the EPSG code (ProjCRS:GeogCRS:Tfm) on that database. \n")
elif map_projn_id_search.rowcount != 0:
#If it exists then print that it exists so the user knows
projn_id = map_projn_id_search.fetchone()
print(f"\n Set {projn_id} as the CRS of the survey. \n")
So, what I needed to do is:
Add a variable like
a = map_projn_id_search.fetchone()
And then make another who could fetch the first result as a string instead of a list
b = a[0]

DbfRead: get the record values from one database and use it in another?

I have this Python program with a for loop(Not EOF loop). Inside the for loop, I assign record values to strSsn and strStoreid, but I want to use them inside another database loop. When I use them AFTER the loop I set them in, I only find the value for the last record in the loop I set them in.
with DBF(r'C:\Sonichr\\empInfo.DBF',lowernames=True,load =True,ignore_missing_memofile=True) as empInfo_table:
for empInfo_rec in empInfo_table:
#Append the records in a list
strSsn = empInfo_rec['ssn']
strStoreid = empInfo_rec['storeid']
s20data_table = DBF('C:\Sonichr\\s20data.DBF',lowernames=True,load =True,ignore_missing_memofile=True)
for s20data_rec in s20data_table:
storeId = s20data_rec['storeid']
ssn = s20data_rec['ssn']
positionde = s20data_rec['positionde']
payrate = s20data_rec['payrate']
if storeId + ssn == strStoreId + strSsn:
if positionde == 'Carhop' :
abraPayRate1 = payrate
else:
abraPayRate0 = payrate
Updated!!

Python List Index Ouf Of Range In IF Statement

So i have multiple patients' information stored in database.txt and i want to retrieve the data from the file into a list.
And the system prompt for patient's id to search and display other information of the patient such as Name, Age, Group & Zone.
However, i'm getting error from line 12, but the similar syntax in line 17 is able to run without problem.
search_keyword = input() # Asks for patient's name or id (either one)
with open("database.txt", "r") as database:
for data in database:
for patients in data.split('|'):
patient_details = []
for details in patients.split(','):
patient_details.append(details)
print(patient_details) # test
print(len(patient_details) # test
print(patient_details.index('Patient001')) # test
print(patient_details[4]) # test
if search_keyword == patient_details[0] or search_keyword == patient_details[4]: # error occured here, where it says list index out of range.
print("Name: " + patient_details[0])
print("Age: " + patient_details[1])
print("Group: " + patient_details[2])
print("Zone: " + patient_details[3])
print("ID: " + patient_details[4]) # no error here, patient_details[4] is able to display patient's id
database.txt
John,18,A,1,Patient001|Nick,20,F,9,Patient002
Test command for line 8,9, 10 and 11:
Line 8: [John, 18, A, 1, Patient001]
Line 9: 5
Line 10: 4
Line 11: IndexError: list index out of range
Can someone explain why this is happening, and any solutions regarding this issue without using any imported modules? Thank you for any assistance.
Imo a very good use-case for a named tuple:
from collections import namedtuple
text = "John,18,A,1,Patient001|Nick,20,F,9,Patient002"
# build database
Patient = namedtuple('Patient', ['name', 'age', 'group', 'zone', 'id'])
db = [Patient(*patient) for entry in text.split("|") for patient in [entry.split(",")]]
# Asks for patient's id
search_keyword = input("Please give an ID: ")
# query the database
result = [patient for patient in db if patient.id == search_keyword]
# or patient.id.startswith(), etc.
print(result)
Without any imported modules, you could use
text = "John,18,A,1,Patient001|Nick,20,F,9,Patient002"
# build database
db = [entry.split(",") for entry in text.split("|")]
search_keyword = input("Please give an ID: ") # Asks for patient's id
# query the database
result = [patient for patient in db if patient[4] == search_keyword]
print(result)
I see no flaw in the code. Although, I can point out a few ways to optimise it :
patient_details = dict()
with open("database.txt", "r") as database:
for data in database:
for patients in data.split('|'):
patients = patients.split(',')
patient_details[patients[4]] = patients[0:4]
search_keyword = input() # Asks for patient's id
if patient_details.get(search_keyword, None):
patient_detail = patient_details[search_keyword]
print("Name: " + patient_detail[0])
print("Age: " + patient_detail[1])
print("Group: " + patient_detail[2])
print("Zone: " + patient_detail[3])
print("ID: " + search_keyword)
Using map instead of a linear search would allow you to search optimally.

Reading data from a file in python, but taking specific data from the file throughout

Hi I am trying to read data from a .dat file, the file has information like this:
1
Carmella Henderson
24.52
13.5
21.76
2
Christal Piper
14.98
11.01
21.75
3
Erma Park
12.11
13.51
18.18
4
Dorita Griffin
20.05
10.39
21.35
5
Marlon Holmes
18.86
13.02
13.36
From this data I need the person number, name and the first number, like so:
1 #person number
Marlon Holmes #Name
18.86 # First number
13.02
13.36
However currently my code is reading the data from the file but not these specific parts, it is simply printing the file
This is my code currently for this specific part:
def Cucumber_Scoreboard():
with open('veggies_2015.dat', 'r') as f:
count = 0
for line in f:
count **= 1
if count % 2 == 0:
print (line)
Im not sure where it's going wrong, I tried to put the data from the file into a list and try it from there but had no success, any help would be greatly appreciated
Whole file code if needed:
def menu():
exit = False
while not exit:
print("To enter new competitior data, type new")
print("To view the competition score boards, type Scoreboard")
print("To view the Best Overall Growers Scoreboard, type Podium")
print("To review this years and previous data, type Data review")
print("Type quit to exit the program")
choice = raw_input("Which option would you like?")
if choice == 'new':
new_competitor()
elif choice == 'Scoreboard':
scoreboard_menu()
elif choice == 'Podium':
podium_place()
elif choice == 'Data review':
data_review()
elif choice == 'quit':
print("Goodbye")
raise SystemExit
"""Entering new competitor data: record competitor's name and vegtables lengths"""
def competitor_data():
global competitor_num
l = []
print("How many competitors would you like to enter?")
competitors = raw_input("Number of competitors:")
num_competitors = int(competitors)
for i in range(num_competitors):
name = raw_input("Enter competitor name:")
Cucumber = raw_input("Enter length of Cucumber:")
Carrot = raw_input("Enter length of Carrot:")
Runner_Beans = raw_input("Enter length of Runner Beans:")
l.append(competitor_num)
l.append(name)
l.append(Cucumber)
l.append(Carrot)
l.append(Runner_Beans)
competitor_num += 1
return (l)
def new_competitor():
with open('veggies_2016.txt', 'a') as f:
for item in competitor_data():
f.write("%s\n" %(item))
def scoreboard_menu():
exit = False
print("Which vegetable would you like the scoreboard for?")
vegetable = raw_input("Please type either Cucumber, Carrot or Runner Beans:")
if vegetable == "Cucumber":
Cucumber_Scoreboard()
elif vegetable == "Carrot":
Carrot_Scoreboard()
elif vegetable == "Runner Beans":
Runner_Beans_Scoreboard()
def Cucumber_Scoreboard():
with open('veggies_2015.dat', 'r') as f:
count = 0
for line in f:
count **= 1
if count % 2 == 0:
print (line)
This doesn't feel the most elegant way of doing it, but if you're going line by line, you need an extra counter in there which results in nothing happening for a set amount of "surplus" lines, before resetting your counters. Note that excess_count only needs to be incremented once because you want the final else to reset both counters, again which will not result in something being printed but still results in a skipped line.
def Cucumber_Scoreboard():
with open('name_example.txt', 'r') as f:
count = 0
excess_count = 0
for line in f:
if count < 3:
print (line)
count += 1
elif count == 3 and excess_count < 1:
excess_count += 1
else:
count = 0
excess_count = 0
EDIT: Based on your comments, I have extended this answer. Really, what you have asked should be raised as another question because it is detached from your main question. As pointed out by jDo, this is not ideal code because it will fail instantly if there is a blank line or missing data causing a line to skip artificially. Also, the new code is stuffed in around my initial answer. Use this only as an illustration of resetting counters and lists in loops, it's not stable for serious things.
from operator import itemgetter
def Cucumber_Scoreboard():
with open('name_example.txt', 'r') as f:
count = 0
excess_count = 0
complete_person_list = []
sublist = []
for line in f:
if count < 3:
print (line)
sublist.append(line.replace('\n', ''))
count += 1
elif count == 3 and excess_count < 1:
excess_count += 1
else:
count = 0
excess_count = 0
complete_person_list.append(sublist)
sublist = []
sorted_list = sorted(complete_person_list, key=itemgetter(2), reverse = True)
return sorted_list
a = Cucumber_Scoreboard()
You could make the program read the whole file line by line getting all the information. Then because the data is in a known format (eg position, name ...) skip the unneeded lines with file.readline() which will move you to the next line.
Someone recently asked how to save the player scores for his/her game and I ended up writing a quick demo. I didn't post it though since it would be a little too helpful. In its current form, it doesn't fit your game exactly but maybe you can use it for inspiration. Whatever you do, not relying on line numbers, modulo and counting will save you a headache down the line (what if someone added an empty/extra line?).
There are advantages and drawbacks associated with all datatypes. If we compare your current data format (newline separated values with no keys or category/column labels) to json, yours is actually more efficient in terms of space usage. You don't have any repetitions. In key/value pair formats like json and python dictionaries, you often repeat the keys over and over again. This makes it a human-readable format, it makes order insignificant and it means that the entire thing could be written on a single line. However, it goes without saying that repeating all the keys for every player is not efficient. If there were 100.000 players and they all had a firstname, lastname, highscore and last_score, you'd be repeating these 4 words 100.000 times. This is where actual databases become the sane choice for data storage. In your case though, I think json will suffice.
import json
import pprint
def scores_load(scores_json_file):
""" you hand this function a filename and it returns a dictionary of scores """
with open(scores_json_file, "r") as scores_json:
return json.loads(scores_json.read())
def scores_save(scores_dict, scores_json_file):
""" you hand this function a dictionary and a filename to save the scores """
with open(scores_json_file, "w") as scores_json:
scores_json.write(json.dumps(scores_dict))
# main dictionary of dictionaries - empty at first
scores_dict = {}
# a single player stat dictionary.
# add/remove keys and values at will
scores_dict["player1"] = {
"firstname" : "whatever",
"lastname" : "whateverson",
"last_score" : 3,
"highscore" : 12,
}
# another player stat dictionary
scores_dict["player2"] = {
"firstname" : "something",
"lastname" : "somethington",
"last_score" : 5,
"highscore" : 15,
}
# here, we save the main dictionary containing stats
# for both players in a json file called scores.json
scores_save(scores_dict, "scores.json")
# here, we load them again and turn them into a
# dictionary that we can easily read and write to
scores_dict = scores_load("scores.json")
# add a third player
scores_dict["player3"] = {
"firstname" : "person",
"lastname" : "personton",
"last_score" : 2,
"highscore" : 3,
}
# save the whole thing again
scores_save(scores_dict, "scores.json")
# print player2's highscore
print scores_dict["player2"]["highscore"]
# we can also invent a new category (key/value pair) on the fly if we want to
# it doesn't have to exist for every player
scores_dict["player2"]["disqualified"] = True
# print the scores dictionary in a pretty/easily read format.
# this isn't necessary but just looks nicer
pprint.pprint(scores_dict)
"""
The contents of the scores.json pretty-printed in my terminal:
$ cat scores.json | json_pp
{
"player3" : {
"firstname" : "person",
"last_score" : 2,
"lastname" : "personton",
"highscore" : 3
},
"player2" : {
"highscore" : 15,
"lastname" : "somethington",
"last_score" : 5,
"firstname" : "something"
},
"player1" : {
"firstname" : "whatever",
"last_score" : 3,
"lastname" : "whateverson",
"highscore" : 12
}
}
"""
Create a function that reads one "record" (5 lines) at a time, then call it repeatedly:
def read_data(in_file):
rec = {}
rec["num"] = in_file.next().strip()
rec["name"] = in_file.next().strip()
rec["cucumber"] = float(in_file.next().strip())
# skip 2 lines
in_file.next()
in_file.next()
return rec
EDIT: improved the code + added a usage example
The read_data() function reads one 5-line record from a file and returns its data as a dictionary. An example of using this function:
def Cucumber_Scoreboard():
with open('veggies_2015.dat', 'r') as in_file:
data = []
try:
while True:
rec = read_data(in_file)
data.append(rec)
except StopIteration:
pass
data_sorted = sorted(data, key = lambda x: x["cucumber"])
return data_sorted
cucumber = Cucumber_Scoreboard()
from pprint import pprint
pprint(cucumber)

Categories