所以,我在做一个项目,在这个项目中,我必须对一个34mb的文本文件进行排序,其中包含了大量的歌曲数据。文本文件的每一行都有年份、唯一编号、艺术家和歌曲。我搞不懂的是如何有效地将数据排序到其他文本文件中。我想按艺人姓名和歌曲名称排序。可悲的是,我只有这些:
#Opening the file to read here
with open('tracks_per_year.txt', 'r',encoding='utf8') as in_file:
#Creating 'lists' to put information from array into
years=[]
uics=[]
artists=[]
songs=[]
#Filling up the 'lists'
for line in in_file:
year,uic,artist,song=line.split("<SEP>")
years.append(year)
uics.append(uic)
artists.append(artist)
songs.append(song)
print(year)
print(uic)
print(artist)
print(song)
#Sorting:
with open('artistsort.txt', 'w',encoding='utf8') as artist:
for x in range(1,515576):
if artists[x]==artists[x-1]:
artist.write (years[x])
artist.write(" ")
artist.write(uics[x])
artist.write(" ")
artist.write(artists[x])
artist.write(" ")
artist.write(songs[x])
artist.write("\n")
with open('Onehitwonders.txt','w',encoding='utf8') as ohw:
for x in range(1,515576):
if artists[x]!= artists[x-1]:
ohw.write (years[x])
ohw.write(" ")
ohw.write(uics[x])
ohw.write(" ")
ohw.write(artists[x])
ohw.write(" ")
ohw.write(songs[x])
ohw.write("\n")
请记住我是个新手,所以请尽量用简单的语言来解释。如果你们还有别的想法,我也想听听。谢谢!你知道吗
你无法战胜pandas的简单性。要读取文件:
然后要按特定列排序并写入新文件,只需执行以下操作:
您可以将数据导入基于词典的结构,即针对每个艺术家和歌曲:
然后在输出时,使用
sorted
按字母顺序获取它们:请尝试这样的方法:
相关问题 更多 >
编程相关推荐