Python Forum

Full Version: Search for Player ID and return games played and number of times
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi team,

New to python and as a gamer, I would like to analyze player data. This would be easy on excel with Vlookup, but I'm having trouble in python.

Here is sample data from two files:

player_game_data.txt
0001 9999 65
0001 8521 5
0002 1234 32
0003 3444 45

Game_id.txt
9999 Fortnite
8521 God of War
1243 Call of Duty
3444 World of Warcraft

Code should function like this...
PlayerID Input: 0001

Print:
Fortnite, 65
God of War, 5


f = open("player_game_data.txt","r")
g = open("game_id.txt", "r")

playerGamesPlayed = input("Please type a player's ID to return what games were played and how many times")
Please help guide me in the right direction.
Hello, welcome to Python and the forums!
It is advisable that instead of using "open" on its own, you use the "with" statement (so-called context manager). You can read about that and how to handle files in the official docs, as well as numerous other sources available online for free.
You will probably want to loop through the file and read each line as a string. Some string methods will come handy, for example splitting the text. After that you will probably be best off saving the read data in a dictionary, or dictionaries.
If you get stuck feel free to post your attempt and explain what the problem is or what the desire result would be compared to actual one.
Is the code below a good starting point on how to open and read each line and save each (key-value pair) to a dictionary? Thank you all!

Player_Dic = {}
with open('player_game_data.txt', 'r') as f:
    for line in f:
        (key, val) = line.split()
        Player_Dic[int(key)] = val


Game_Data = {}
with open('game_id.txt', 'r') as a:
        (key, val) = line.split()
        Game_Data[int(key)] = val

# Code that loops throught the file and saves each line as a string


# Below is the user input that will search for the Key Id on the list.
ListenersTimesPlayed = input("Type Listener ID")
ListenersTimesPlayed = int(Artist_Played)
Will I used a nested dictionary to map the first list element in [1] position to to the element in [0] position on the second list.

player_game_data.txt
0001 9999 65
0001 8521 5
0002 1234 32
0003 3444 45

Game_id.txt
9999 Fortnite
8521 God of War
1243 Call of Duty
3444 World of Warcraft

When I print, the result would give me
Fortnite, 65
God of War, 5