I have many list containing thousands of line of data. I want to merge those list side by side in a .dat file like this 1::2::F::4::990
. Consider these single data as different list I want to merge the data exactly as this. Can you help
I have done this so far.
with open("mergefile.dat", "w") as outfile:
outfile.write("\n".join(users_id) '::' '\n'.join(movie_id) '::' '\n'.join(ratings) '::' '\n'.join(timestamps) '::' '\n'.join(genders) '::' '\n'.join(age) '::' '\n'.join(occupation) '::' '\n'.join(zipp) '::' '\n'.join(moviename) '::' '\n'.join(yearOfMovie))
I want to merge data like this and i have list like user_id, movie_id, ratings, etc each column is a list
this is how my list looks like
These are my sample lists
user_id=['1','2', '3','4', '5','6','7']
movie_id=['1246','1247', '2081', '1240', '714']
ratings=['5','6','2','7']
timestamp=['9999','0000','5555','2222','1111']
gender=['F','G','F','F','G']
age=['1','1','2']
occupation=['10','10','10','10','10','10']
zip=['67890','45600']
title=['titl1','title2']
genres=['genre1','genre2','genre3']
year=['1999','1990']
i have tried this and didn't got what i want
for user,movie,rating,timestamp,gender,age,occupations,zipps,title,genre,year in zip(users_id,movies_id,ratings,timestamps,genders,age,occupation,zipp,moviename,genres,yearOfMovie):
with open("mergefile1.dat", "w") as outfile:
outfile.write(user '::' movie '::' rating '::' timestamp '::' gender '::' age '::' occupations '::' zipps '::' title '::' genre '::' year)
this is my output only one line
1::1246::4::978302091::F::1::1::48067::Toy Story::Animation|Children's|Comedy::1995
CodePudding user response:
In the example you posted there are various issues, the names of the lists and variables, the lack of new line (\n), also you don't need to open the file each time. And please notice that the zip iterator only iterates the least number of elements when the lists are of different lengths. Using your example but fixing the mentioned issues, this works:
user_ids=['1','2', '3','4', '5','6','7']
movie_ids=['1246','1247', '2081', '1240', '714']
ratings=['5','6','2','7']
timestamps=['9999','0000','5555','2222','1111']
genders=['F','G','F','F','G']
ages=['1','1','2']
occupations=['10','10','10','10','10','10']
zipps=['67890','45600']
titles=['titl1','title2']
genres=['genre1','genre2','genre3']
yearOfMovie=['1999','1990']
with open("mergefile1.dat", "w") as outfile:
for user,movie,rating,timestamp,gender,age,occupation,zipp,title,genre,yearof in zip(user_ids,movie_ids,ratings,timestamps,genders,ages,occupations,zipps,titles,genres,yearOfMovie):
outfile.write(user '::' movie '::' rating '::' timestamp '::' gender '::' age '::' occupation '::' zipp '::' title '::' genre '::' yearof '\n')
output:
1::1246::5::9999::F::1::10::67890::titl1::genre1::1999
2::1247::6::0000::G::1::10::45600::title2::genre2::1990