Python Forum
help with extracting and matching values in a text file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help with extracting and matching values in a text file
#1
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()
Reply
#2
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).
Reply
#3
(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?
Reply
#4
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.
Reply
#5
(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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Extracting link list to json file naor 5 2,626 Sep-17-2020, 04:16 PM
Last Post: micseydel
  Convert text from an image to a text file Evil_Patrick 5 4,300 Jul-30-2019, 07:57 PM
Last Post: DeaD_EyE
  reading text file and writing to an output file precedded by line numbers kannan 7 10,413 Dec-11-2018, 02:19 PM
Last Post: ichabod801
  Extracting variable values from labels on csv file using Python Laura 1 2,218 Nov-12-2018, 06:54 PM
Last Post: ichabod801
  file a table of values Ybivashka322 4 3,719 Dec-14-2017, 06:11 PM
Last Post: mpd
  How to find exact matching string from the text desul 5 4,112 Apr-17-2017, 04:31 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020