'Using CSV module next() and storing the data in a dictionary to go back a row
I have a PYQT5 game for sports and I am using a CSV file for the data I would like to allow the user to go back and forward rows (games) when there is more than one game on the same day(date).
At the moment I am only using the next() from the CSV module this works fine and in the next_game() method I can access the data from the row and update my labels on the UI. But when I looked for a previous() or a similar method I could not find one
def next_game():
next_Game = next(csvreader_fix)
self.next_match.clicked.connect(lambda: next_game())
I would like to allow the user to have a back button and update the UI to the previous game.
I was thinking of storing the game data in a dictionary every time next_game() is called and using the keys to display the data for each game on the UI. I think that this will also boost performance as I will be using hashing rather than reading data from a CSV file when the user switches games and the UI is updated.
The key's will be the game number in the season so it will be easy to subtract or add 1 when a button is pressed and iterate through the dictionary.
Once the user has finished looking at that days matches the data can be discarded from the dictionary and the next days games would be placed in the dictionary every time `next_game()` is called.
Am I over complicating things and there is an easier way of achieving what I am trying to using pandas another module?
At the moment I am reading the csv file like so
csvreader_fix = csv.reader(fixtures)
fix = next(csvreader_fix)
In the 'next_game()' I access each index in the row that I need like this
def next_game():
next_Game = next(csvreader_fix)
result = next_Game[6]
home_team = next_Game[2]
away_team = next_Game[3]
Solution 1:[1]
One option would be to use the pandas module which has more functionality.
But if you really want to use this module, you can convert the whole CSV file into a list and then access the rows in a way as you would access a list.
csvr = csv.reader(file)
game_data = list(csvr)
i = 0 # Or 1, if you have a header in your csv
game = game_data[i]
def next_game():
i += 1
next_Game = game_data[i]
def previous_game():
i -= 1
previous_Game = game_data[i]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mbostic |
