Home > Software engineering >  How can I take the string of names and preferences & add them to the dictionary with names as the ke
How can I take the string of names and preferences & add them to the dictionary with names as the ke

Time:10-24

My code as it is right now looks like this:

def read_in_movie_preference():
    """Read the move data, and return a 
    preference dictionary."""
    preference = {}
    movies = []
    
    # write code here:
    
    file_location="./data/"
    f = open(file_location "preference.csv","r")
    df = f.readlines()
    
    #names as keys and prefrences
    for line in df:
        name = line[1].strip("\n").split(",")
        prefs = line[2:].strip("\n").split(",")
        preference[line[1]] = line[2:]
    
    #print(test)
    
    #movie names`
    movietitles = df[0].strip("\n").split(",")
    for movie in movietitles:
        movie=movie.rstrip()
    #can't seem to get rid of the spaces at the end    
    movies =movietitles[2:]
            
        
    print(movies)
       
    return [movies, preference]

I cant seem to get the movie titles into the list without spaces at the end of some of them & I also cant add the names and preferences into the dictionary... I am supposed to do this task with basic python and no pandas .. very stuck would appreciate any help!

the dictionary would have names as keys and the preference numbers in number format instead of strings so it would theoretically look like this: key: pref: dennis, 0 1 0 1 0 ect

enter image description herethis is what the data set looks like

here is the data pasted:

Timestamp,Please fill in your name. (You can also use an alias name). ,The Shawshank Redemption,The Godfather,The Dark Knight ,Star Wars: The Force Awakens,The Lord of the Rings: The Return of the King,Inception,The Matrix ,Avengers: Infinity War ,Interstellar ,Spirited Away,Coco,The Dark Knight Rises,Braveheart,The Wolf of Wall Street,Gone Girl ,La La Land,Shutter Island,Ex Machina,The Martian,Kingsman: The Secret Service
9/30/20 17:04,Test Student Long Name,0,0,1,1,0,1,0,1,1,1,0,0,0,1,0,0,0,0,1,0
9/30/20 9:53,Test Student,0,0,1,1,-1,0,0,1,1,1,0,0,0,1,0,0,0,0,0,0
9/30/20 9:53,Jiaxu Rong,1,1,1,0,0,0,1,1,1,1,1,1,0,1,0,1,0,0,1,1
9/30/20 9:53,Trevor,1,1,1,0,0,1,1,0,1,-1,0,1,0,0,0,0,0,0,1,0
9/30/20 9:53,Guokai,0,0,1,0,1,0,0,1,0,0,1,-1,0,1,0,1,0,0,0,1
9/30/20 9:54,Yanzhang Cao,0,-1,0,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0
9/30/20 9:54,Kongliang Chen,0,0,1,0,1,0,0,1,0,0,1,1,0,1,0,1,0,0,0,1
9/30/20 9:54,Jason,1,0,1,0,-1,1,0,1,1,0,0,1,1,1,-1,0,0,0,-1,1
9/30/20 9:54,Q,0,1,-1,0,1,1,1,0,0,1,0,0,0,1,1,0,1,1,0,1
9/30/20 9:55,Eric,1,1,1,1,1,1,1,1,0,1,0,1,1,1,0,0,1,1,1,1
9/30/20 9:55,Elaine Jiang,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0
9/30/20 9:55,Songqi Fan,1,0,1,1,1,0,0,1,1,0,1,1,0,1,0,1,0,0,0,0
9/30/20 9:55,Jacob Scheinman,1,1,1,1,1,1,1,1,-1,0,1,1,0,1,0,0,0,0,1,1
9/30/20 9:56,Ziyi Qian,0,0,0,1,1,1,0,1,1,0,1,0,0,0,0,0,1,0,1,1
9/30/20 9:56,Huanyue Liao,1,-1,-1,1,1,1,1,-1,0,1,1,0,1,0,0,0,1,0,0,-1
9/30/20 9:56,Yitong,0,0,0,-1,0,1,0,0,1,1,1,0,0,0,0,-1,0,0,1,1
9/30/20 9:56,qimeng luo,1,1,1,0,0,0,0,1,1,1,1,1,0,1,0,-1,-1,-1,1,-1
9/30/20 9:59,Haijian Huang,1,1,0,0,0,1,0,-1,1,1,1,0,0,1,0,1,0,0,-1,1
9/30/20 9:59,Yunong Tian,0,1,0,0,1,1,1,1,1,1,0,0,1,1,1,0,1,0,1,1
9/30/20 10:00,Dennis Zhang,1,0,1,1,0,1,1,1,1,1,0,1,0,0,0,0,0,1,1,0
9/30/20 10:00,Ricky Gui,1,1,1,0,1,1,1,1,1,1,1,1,0,1,0,1,1,0,1,1
9/30/20 10:01,Harry You,0,0,0,0,0,1,1,1,0,0,1,0,0,0,0,0,0,0,0,1
9/30/20 10:02,Mingda Huang,0,0,1,0,0,1,1,1,1,1,1,1,1,1,0,0,1,0,1,1
9/30/20 10:02,Robert Chen,0,0,-1,-1,-1,1,1,1,1,1,0,-1,0,1,1,1,1,1,1,1
9/30/20 10:04,Elsa Wang,1,1,1,1,0,1,1,1,0,-1,0,1,0,1,0,-1,1,0,-1,1
9/30/20 10:06,Hao,1,1,0,0,1,1,1,0,1,1,1,0,1,1,-1,1,-1,0,1,1
9/30/20 10:31,Nobody,0,0,0,0,1,-1,0,0,0,1,0,0,0,1,0,1,0,0,0,0
9/30/20 14:02,Devlin Sullivan,1,1,1,0,0,1,0,-1,0,1,0,1,0,1,1,0,1,0,1,0
9/30/20 14:03,Michael Blau,1,0,1,0,-1,1,0,1,1,0,0,1,0,1,0,1,0,1,1,1
9/30/20 14:04,Grace,0,0,0,1,-1,0,0,1,0,1,1,0,0,1,1,1,0,0,0,0
9/30/20 14:04,Sam Andelman,1,1,1,-1,1,1,1,1,1,0,0,1,0,1,0,0,0,0,1,1
9/30/20 14:06,Liza Fried  ,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,1,0,0,0,0
9/30/20 14:07,Kelsey ,1,0,-1,0,0,1,0,0,1,1,0,0,0,1,-1,1,0,0,0,0
9/30/20 14:07,Lucas,0,0,0,1,1,1,1,1,1,0,1,0,0,1,0,0,0,0,1,1
9/30/20 14:09,Vi Krstic,0,0,0,0,0,1,1,0,1,1,1,0,-1,1,1,0,0,-1,0,1
9/30/20 14:10,Andrew,1,0,1,-1,-1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1
9/30/20 14:20,Kiri ,1,-1,1,1,1,0,0,1,1,1,0,1,0,1,0,1,0,0,0,1
9/30/20 14:29,ag,0,0,1,0,-1,1,1,1,1,0,0,1,0,1,0,0,0,1,1,1
9/30/20 14:32,Joe Stover,1,1,1,0,0,1,0,-1,0,0,1,1,0,1,0,1,0,1,1,0
9/30/20 16:10,Joe Stover,1,1,1,0,0,1,0,-1,0,0,1,1,0,1,0,1,0,1,1,0
9/30/20 16:39,Gary,1,0,1,0,1,1,0,1,1,0,0,1,0,1,0,0,-1,0,1,0
9/30/20 16:42,Michael Yin,1,0,1,0,0,0,1,1,0,0,0,1,-1,1,0,0,0,0,0,0
9/30/20 16:51,Julia Chandonnet,0,1,-1,-1,-1,1,1,1,1,0,0,0,0,0,1,1,0,0,1,0
9/30/20 17:04,Michael Treiber,0,0,1,1,0,1,0,1,1,1,0,0,0,1,0,0,0,0,1,0
9/30/20 17:50,Joanna Lycroft,0,1,1,1,1,1,1,1,1,1,0,-1,0,-1,0,1,0,1,1,0
9/30/20 18:18,Jared Fong,1,1,1,1,0,1,1,1,1,0,-1,1,0,1,0,0,1,-1,1,0
9/30/20 18:23,Nolan Stafford,1,1,1,1,1,1,1,1,1,0,1,1,0,1,0,0,1,0,1,1
9/30/20 20:02,kailin shi,1,0,1,-1,0,0,1,0,0,1,1,1,1,1,1,1,0,0,0,0
9/30/20 20:02,Grant,1,-1,1,-1,1,0,0,1,0,1,1,1,1,1,0,1,0,0,0,1
9/30/20 20:02,Cathy Xie,0,1,0,0,0,1,1,-1,1,0,1,1,1,1,0,1,0,0,0,1
9/30/20 20:03,Honghua Su,1,0,1,1,1,0,1,1,1,1,1,1,1,0,0,1,0,0,-1,1
9/30/20 20:03,Bryan Peltier,1,1,1,-1,0,1,0,1,1,0,0,1,1,0,0,0,0,0,1,1
9/30/20 20:04,Yueqi Qian,0,1,0,1,0,1,0,1,1,0,-1,0,0,1,0,0,0,0,0,0
9/30/20 20:05,Feier Li,1,1,0,1,1,1,0,-1,0,1,1,0,1,0,-1,1,0,0,0,-1
9/30/20 20:05,Zhixin Liu,-1,-1,-1,1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,1,1,-1,-1,-1,-1
9/30/20 20:06,jasmine,0,0,0,0,0,0,0,1,1,1,0,0,0,0,1,0,1,0,1,1
9/30/20 20:06,Chang Mu,1,1,1,0,1,1,0,1,1,0,1,0,0,0,0,1,0,0,0,1
9/30/20 20:08,Chen Wen,0,0,1,1,0,1,1,1,0,1,0,0,0,0,-1,0,0,0,0,1
9/30/20 20:11,zirui zhu,1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
9/30/20 20:12,Yutao Jiang,1,0,1,1,1,1,1,1,1,1,1,1,0,1,0,0,1,1,1,1
9/30/20 20:13,Bo,1,0,1,0,0,1,0,1,0,-1,1,1,0,1,1,0,1,0,0,1
9/30/20 20:30,Antonia Weng,1,-1,0,0,1,1,1,1,-1,1,1,0,0,0,1,-1,-1,0,0,1
9/30/20 20:33,Sean Peng,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,1,0,0,1,0
9/30/20 20:36,Geeeee,1,1,1,1,1,0,0,1,0,0,0,1,1,1,0,1,0,0,0,1
9/30/20 20:37,Peyton Wilson,1,0,1,-1,1,1,1,1,1,-1,0,1,0,1,0,0,1,1,1,1
9/30/20 20:41,Qihong Li ,1,0,1,0,1,1,0,0,0,1,1,1,0,0,0,0,0,0,1,0
9/30/20 20:44,Cathy Xie,0,1,0,0,0,1,1,-1,1,0,1,1,1,1,0,1,0,0,0,1
9/30/20 21:02,Haochen Zhou,1,1,1,1,0,0,1,1,0,0,0,1,0,1,0,1,0,0,1,1
9/30/20 21:15,Emma,0,0,1,0,1,0,0,1,-1,0,1,1,0,1,-1,0,0,0,1,0
9/30/20 21:15,Thomas,1,-1,1,-1,1,1,1,1,-1,-1,-1,1,1,1,-1,-1,-1,-1,-1,-1
9/30/20 21:18,Aurora Chen,1,0,0,0,0,1,0,1,1,1,1,0,0,-1,1,-1,0,0,0,1
9/30/20 21:21,Nexus7,1,1,1,-1,1,0,1,1,0,1,1,1,0,1,1,0,0,-1,0,1
9/30/20 21:22,Joy Hou,0,0,-1,0,1,1,0,1,1,1,1,1,0,0,1,0,1,0,0,1
9/30/20 21:23,Yuheng Zhuo,1,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
9/30/20 21:24,Yitsukii,1,1,0,0,1,1,1,-1,1,1,1,-1,1,0,0,1,0,0,1,0
9/30/20 21:26,Baiyu Chen,0,1,0,0,0,0,1,-1,1,0,1,0,1,1,0,1,0,0,0,1
9/30/20 21:27,Tiffany,1,0,0,1,0,0,0,1,1,1,1,0,0,0,0,1,0,0,0,1
9/30/20 21:29,Xul,1,0,1,-1,1,1,1,0,1,1,0,0,-1,1,1,1,1,0,1,0
9/30/20 21:32,xianxian,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0
9/30/20 21:33,Vicky,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0
9/30/20 21:34,Marilyn,0,1,0,0,1,0,1,0,0,-1,1,0,0,0,0,1,0,0,0,0
9/30/20 21:49,Albert Kao,0,0,0,1,0,0,0,1,1,0,1,0,0,1,0,1,0,0,0,0
9/30/20 21:50,Sahil Rastogi,0,1,1,1,1,-1,0,1,1,-1,1,1,1,0,0,-1,0,0,1,1
9/30/20 21:52,Tianyu Wu,1,0,0,0,1,0,0,0,0,0,0,-1,1,1,1,1,0,1,-1,1
9/30/20 21:59,Sarah Chang,1,0,0,-1,0,1,0,1,-1,1,1,1,0,0,-1,1,0,0,1,1
9/30/20 22:11,Andy Mu,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
9/30/20 22:38,Avatar,0,1,1,0,0,-1,-1,1,-1,0,1,1,0,1,0,0,0,0,0,0
9/30/20 22:48,Nolan Stafford,1,1,1,1,1,1,1,1,1,0,1,1,0,1,0,0,1,0,1,1
9/30/20 23:17,Daiying Wu,1,0,0,0,1,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0
9/30/20 23:18,Fengnian Zhou,1,-1,1,-1,-1,1,1,-1,1,1,1,1,-1,1,1,1,-1,1,1,1
9/30/20 23:50,Alex,1,0,1,0,0,1,1,1,1,1,1,1,0,1,1,0,1,-1,1,1
10/1/20 0:05,DJZ2,0,1,0,-1,1,1,0,1,0,0,1,0,0,-1,0,1,0,0,0,0
10/1/20 1:01,Honghua Su,1,0,1,1,1,0,1,1,1,1,1,1,1,0,0,1,0,0,-1,1
10/1/20 1:03,Kelsey ,1,0,-1,0,0,1,0,0,1,1,0,0,0,1,-1,1,0,0,0,0
10/1/20 1:51,xingxu,0,1,0,0,0,0,1,1,0,1,0,0,0,-1,0,0,1,-1,-1,1
10/1/20 3:51,Yujia Zhang,1,1,1,1,1,0,-1,1,1,-1,-1,1,1,-1,-1,-1,0,-1,1,1
10/1/20 5:14,Connor Lotz ,0,0,1,1,0,0,0,1,0,0,0,1,0,1,0,0,0,0,1,1
10/1/20 5:39,Tianyi Liu,1,1,1,0,0,1,0,1,0,1,0,1,0,1,0,1,1,0,1,0
10/1/20 8:32,Haoyuan Tan,0,0,1,0,1,0,0,1,0,0,0,1,0,1,0,0,0,0,0,0
10/1/20 9:47,Digmel ,0,0,1,1,0,0,-1,1,1,0,1,1,0,1,0,0,1,1,-1,1
10/1/20 10:25,Rhea Pillai,1,0,1,0,0,1,0,1,1,0,0,1,0,0,0,0,0,0,0,1
10/1/20 10:49,Monica,0,0,0,0,0,1,1,-1,1,1,1,0,0,1,0,-1,0,0,0,0
10/1/20 11:36,Kun Yu,1,0,1,0,0,0,1,0,1,1,1,1,0,0,0,0,0,0,1,1
10/1/20 12:37,Rahul Parthasarathy,1,0,1,1,1,1,0,1,1,0,0,1,0,1,0,0,0,0,1,1
10/1/20 13:31,vivi,1,0,1,1,1,1,1,1,1,1,0,1,1,0,0,0,1,0,1,1
10/1/20 13:51,Imani,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0
10/1/20 16:00,Henry Singer,1,1,1,1,0,1,1,1,1,0,0,1,0,1,0,0,1,0,1,1
10/1/20 16:32,Juliana Beal,0,1,0,0,-1,0,0,0,-1,0,1,0,0,0,0,1,0,0,0,0
10/1/20 16:38,Juncen Yu,-1,0,1,1,1,0,0,-1,1,1,1,1,0,-1,0,0,0,0,1,1
10/1/20 21:43,Yizhou,1,1,1,1,1,1,1,1,1,0,0,1,0,1,1,-1,1,0,1,1
10/1/20 22:52,Nora,1,0,0,0,-1,0,0,1,0,1,1,0,0,0,0,1,0,0,1,0
10/2/20 3:33,Yufei,0,0,0,0,1,1,0,1,1,1,1,0,0,0,0,1,0,0,0,1
10/2/20 9:57,Shaowen Ni,1,1,1,-1,0,1,1,1,-1,0,-1,1,1,1,1,1,1,0,0,1
10/2/20 10:45,Xuan Li,0,0,-1,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,1
10/2/20 11:54,Jarrett Dykema,1,1,1,1,1,1,0,1,1,-1,0,1,1,1,1,0,1,1,1,1
10/2/20 18:42,Emma,0,0,1,0,1,0,0,1,-1,0,1,1,0,1,-1,0,0,0,1,0
10/2/20 20:03,Agam,0,0,1,0,0,1,0,1,1,0,1,1,0,1,0,0,1,1,0,0
10/2/20 23:15,Matt LaVersa,0,0,1,-1,1,1,1,1,1,0,0,1,0,0,0,1,0,1,1,0
10/2/20 23:40,Lingyi Ye,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,-1,0,0,0,0
10/3/20 7:37,Crystal,1,0,1,0,0,1,1,1,1,0,1,1,0,0,1,1,0,0,0,0
10/3/20 9:25,Anthony Conti,1,1,1,1,1,1,1,1,1,0,0,1,1,1,0,0,1,1,1,1
10/3/20 10:09,Sen Zhu,0,0,0,1,-1,0,0,-1,0,1,0,-1,0,-1,0,0,0,0,0,0
10/3/20 13:11,Lujiang Chen,0,0,1,1,1,1,1,1,1,1,1,1,0,1,1,0,1,1,1,1
10/3/20 15:57,Katie,0,1,-1,-1,0,0,1,0,1,0,-1,0,0,1,1,1,0,0,0,0
10/3/20 18:17,Jack S. ,1,1,1,-1,1,1,1,0,0,1,0,1,-1,1,1,1,1,1,1,1
10/3/20 19:50,Elizabeth A.,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,-1,0,0,0,0
10/3/20 20:42,Crystal,1,0,1,0,0,1,1,1,1,0,1,1,0,0,1,1,0,0,0,0
10/4/20 1:07,Jason,1,0,1,1,1,1,1,1,1,1,1,1,0,1,0,0,0,0,1,0
10/4/20 2:17,Lizz Cao,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0
10/4/20 6:14,Minglun Yin,1,0,0,1,1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0
10/4/20 7:11,Tracy,0,0,-1,1,0,0,0,1,0,0,1,-1,0,1,-1,1,-1,0,0,1
10/4/20 9:51,Zhongkun Guo,1,1,1,0,1,1,0,1,1,1,0,1,0,-1,1,1,0,0,0,-1
10/4/20 13:19,Matthew Yang,0,-1,-1,1,0,1,0,1,0,1,1,1,0,-1,0,0,0,0,1,1
10/4/20 15:45,Beth,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0
10/4/20 18:42,Maya ,0,0,0,1,0,0,0,1,0,-1,1,0,0,0,0,1,0,0,0,0
10/4/20 19:09,Yifan He,1,1,1,-1,1,1,1,1,1,1,1,1,0,1,0,0,0,0,1,1
10/4/20 21:40,Ziqing Ouyang,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,1,0,0,0,1
10/5/20 0:10,Johnnie Teng,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0
10/5/20 0:34,shaohua,1,1,1,0,1,1,1,1,1,1,1,1,0,0,1,0,1,0,1,1
10/5/20 5:16,Yuchen,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
10/5/20 8:24,Jingze Huang,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
10/5/20 18:42,Sara,1,1,0,0,0,1,0,0,1,1,0,0,1,0,0,-1,1,0,1,0
10/5/20 19:49,Duoyuan Wang,0,0,1,-1,0,1,-1,1,1,1,1,-1,0,0,0,0,0,0,-1,0
10/5/20 21:39,Spencer,0,1,1,1,0,0,0,1,0,0,0,1,0,1,0,0,0,0,0,0
10/5/20 22:28,Spencer,0,1,1,1,0,0,0,1,0,0,0,1,0,1,0,0,0,0,0,0
10/6/20 2:02,Spencer,0,1,1,1,0,0,0,1,0,0,0,1,0,1,0,0,0,0,0,0
10/6/20 12:34,Chance Bayles ,1,0,1,-1,-1,1,1,-1,1,1,0,1,1,1,1,1,-1,-1,1,-1
10/6/20 13:03,Jackson Brodwolf,1,0,1,0,-1,1,0,1,1,-1,-1,1,0,1,1,0,1,-1,1,1
10/6/20 13:42,Lauren,0,0,0,0,0,0,0,1,-1,0,1,0,0,0,0,1,0,0,1,1
10/6/20 20:37,Steve,0,1,0,1,1,1,1,1,0,0,0,0,1,1,0,0,0,1,0,0
10/6/20 21:38,Claire Huang,1,-1,-1,-1,-1,1,-1,-1,-1,1,1,-1,-1,1,-1,1,-1,-1,-1,-1
10/6/20 21:39,Alexander Hazel,1,0,1,1,0,1,1,1,1,0,1,1,0,1,1,1,0,0,1,1
10/6/20 23:12,Joanne Lin,0,0,0,1,0,0,0,1,0,0,1,0,0,0,0,1,0,0,0,0
10/6/20 23:45,Jared Katzelnick,1,0,1,-1,0,1,0,1,1,0,1,1,0,1,0,0,0,0,1,1
10/7/20 11:16,Kati,0,0,-1,-1,-1,1,1,0,-1,1,1,-1,1,1,1,1,1,0,0,0
10/7/20 11:59,Ty,1,1,1,1,0,1,0,1,1,0,0,1,1,1,0,1,1,0,1,1
10/7/20 13:58,Chang Mu,1,1,1,0,1,1,0,1,1,0,1,0,0,0,0,1,0,0,0,1
10/9/20 21:40,Ziqing Ouyang,0,0,1,0,0,1,1,1,0,0,0,0,0,0,0,1,0,0,0,1
10/7/20 13:42,Lauren,1,0,0,0,0,0,0,1,-1,0,1,0,0,0,0,1,0,0,1,1
10/9/20 21:39,Spencer,1,1,1,1,0,0,0,1,0,0,0,1,0,1,0,0,0,0,0,0

CodePudding user response:

So the issue here is that you are using rstrip on a copy of the data but never apply it to the original.

The issue

for movie in movietitles:
    movie=movie.rstrip() # Changes the (copy) of the data rather than the original
    # We still need to apply this back to movietitles

There are a couple ways to fix this!

# Using indexing
for _ in range(len(movietitles)):
    movietitles[_] = movietitles[_].rstrip()

Or we can do this inline with list comprehension

# Using list comprehension
movietitles = [movie.rstrip() for movie in movietitles]

As stated in the other answer, when working with csv data it's recomended to use a csv parser, but completely unnecessary for this scale! Hope this helps

CodePudding user response:

You need to use a proper csv parser for this.

from csv import DictReader

with open('preferences.json') as file: # replace your file path here
    for row in DictReader(file):
        print(row)

Output (cropped some values for brevity)

{'Timestamp': '10/9/20 21:39',
 'Please fill in your name. (You can also use an alias name). ': 'Spencer',
 'The Shawshank Redemption': '1',
 'The Godfather': '1',
 'The Dark Knight ': '1',
 'Star Wars: The Force Awakens': '1',
 'The Lord of the Rings: The Return of the King': '0',
 'Inception': '0',
 'The Matrix ': '0',
 'Avengers: Infinity War ': '1',
 'Interstellar ': '0',
 'Spirited Away': '0',
 'Coco': '0',
 'The Dark Knight Rises': '1',
 'Braveheart': '0',
 'The Wolf of Wall Street': '1',
 'Gone Girl ': '0',
 'La La Land': '0',
 'Shutter Island': '0',
 'Ex Machina': '0',
 'The Martian': '0',
 'Kingsman: The Secret Service': '0'}

For the format you want

from csv import DictReader

res = []

with open('preferences.json') as file:
    for row in DictReader(file):
        name = row.pop('Please fill in your name. (You can also use an alias name). ')
        row.pop('Timestamp')
        res.append({name: list(map(int, row.values()))})

print(res)

Output (cropped some values for brevity)

[{'Lauren': [1, 0, 0, 0, 0, 0, 0, 1, -1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1]},
 {'Spencer': [1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0]}]
  • Related