Python Forum

Full Version: help with extracting and matching values in a text file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have 2 text files and I need to grab a value from fileA, match it with fileB and print the result.

fileA contains userID, artistID and how many times artistID has been played. fileB contains artistID and the name of the artist.

I need to write a function that will tell me how many times a certain user has played an artist. I need to print the name of the artist and the number of times. I got the pseudocode done but having trouble moving forward.

def printListenersTimesPlayed(userID)
find all artists and number of time they've been played from fileA
find the name of the artist from fileB
print list of artists and times that the user (userID) has played

when someone types a userID (example: 1000002) it should print out for example Pearl Jam, 55.

any help is appreciated!

and here's what i have so far:

file1 = open("user_artist_data.txt", "r")
file2 = open("artist_data.txt", "r")

users = [line.split(' , ')for line in file1]
print(file1)

artist = [line.split(' , ') for line in file2]
print(file2)

#for values in file1, grab the same value from file2 and print the result.

def printListenersTimesPlayed():
    print()
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.
I would go about this by storing data you read in a dictionary, or dictionaries. That way you can easily access data by key (in your case user ID).
(May-03-2018, 05:38 AM)j.crater Wrote: [ -> ]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.
I would go about this by storing data you read in a dictionary, or dictionaries. That way you can easily access data by key (in your case user ID).
can you show an example of the dictionary please?
There is a great tutorial on our forums.
Here is the entry on dictionaries in official Python tutorial.
Here is one of many tutorials found online.

Feel free to post your coding attempt if you get stuck or need clarification on something specific.
(May-03-2018, 05:44 AM)j.crater Wrote: [ -> ]There is a great tutorial on our forums.
Here is the entry on dictionaries in official Python tutorial.
Here is one of many tutorials found online.

Feel free to post your coding attempt if you get stuck or need clarification on something specific.

am i on the right track?
with open("user_artist_data.txt") as file1:
    read_data = file1.read()
with open("artist_data.txt") as file2:
    read_data = file2.read()

userID = {}
artistID = {}

users = [line.split(' , ')for line in file1]
print(file1)

artist = [line.split(' , ') for line in file2]
print(file2)
how do i take the value in file1 and match it with file2 and print out the result? that's where i am stuck :/

anyone?