为什么python不会附加到这个文件?

时间:2014-01-10 14:08:56

标签: python file file-io

所以,我正在研究这个项目,我有一个34mb的文本文件,里面装满了歌曲数据。每行都有一个年份,艺术家,唯一编号和歌曲,用字符串<SEP>分隔。现在,我已将每个事物分类到不同的列表中。我现在要做的是将艺术家分成不同的文本文件。问题是python会创建文件但不会打印到它,文件大小为0字节。这是我的代码:

#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('artistssorted.txt', 'a') as artist:

    for x in range(1000000):
        x=1
        if artists[x-1]==artists[x]:
            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")
        else:
            x=x+1

仅供参考,uics =唯一标识符代码 另外,如果你们有关于如何对这个文件进行排序的任何其他建议,我很高兴听到它。请记住,我是一个新手。

2 个答案:

答案 0 :(得分:2)

如果前两个艺术家条目不相等,则您的条件if artists[x-1]==artists[x]:将始终为假,因为您为每个循环迭代覆盖x为1。在文件中写入将永远不会发生。

使用范围迭代时,变量会自动递增,因此无需自行完成。

答案 1 :(得分:1)

这是我的热门:

#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
    records = []

    #Filling up the 'lists'
    for line in in_file:
        year, uic, artist, song=line.split("<SEP>")
        records.append((artist, year, uic, song))

#Sorting:
records.sort()
with open('artistssorted.txt', 'a') as artist_file:

    for (artist, year,uic,song) in records:
        artist_file.write("%s %s %s %s\n"%(year, uic, artist, song))