Guys help will be very grateful, I suffer for 2 days, I am a beginner in Python There is a csv file:

Pink Floyd | The Dark Side Of The Moon | 1973 | psychodelic rock | 43:00 Eminem | Recovery | 2007 | rap | 21:00 

The task is to create a list,

It is necessary to create a tuple in which there will be 2 more tuples. In the first tuple - the name and in the second tuple - information. All external tuples must be in one big list. After that, you will need to find different elements from the list by name and show all the information of this element.

Example:

 music = [(("Pink Floyd", "The Dark Side Of The Moon"), (1973, "psychodelic rock", "43:00")), (("Britney Spears", "Baby One More Time"), (1999, "pop", "42:20"))] 

Here is what I am trying to do:

 import csv with open('music.csv', 'r') as f: read_csv = csv.reader(f, delimiter="|") read_csv music = [] for row in read_csv: music.append(row) 

Lists out in the list

 [['Pink Floyd ', ' The Dark Side Of The Moon ', ' 1973 ', ' psychodelic rock ', ' 43:00'], ['Eminem ', ' Recovery ', ' 2007 ', ' rap ', ' 21:00']] 

How to insert these two tuples and take them into one common tuple? and how to search further for words on tuples?

    1 answer 1

    You talk about tuples, but for some reason you work with lists. I do not understand. Want tuples - make tuples!

     import csv tuples = [] with open('music.csv', 'r') as f: read_csv = csv.reader(f, delimiter="|") for row in read_csv: row = tuple(row) tuples.append(row) tuples = tuple(tuples) for x in tuples: print(x) print(type(x)) print(type(tuples)) 

    And your task can be solved like this:

     import csv music = [] with open('music.csv', 'r') as f: read_csv = csv.reader(f, delimiter="|") for row in read_csv: artist = row[0] album = row[1] year = row[2] genre = row[3] time = row[4] nametuple = (artist, album) infotuple = (year, genre, time) globaltuple = (nametuple, infotuple) music.append(globaltuple) print(music) 
    • I understand the logic of your code - Valek Potapov
    • @ValekPotapov updated the answer - kitscribe
    • Thank you very much! It seems so simple, but I could not think of it. Very much helped me out! Grateful - Valek Potapov