Python:将计数器附加到csv文件

时间:2015-12-07 15:23:30

标签: python csv append

我正在处理一个项目,其中包含我从last.fm收集的数据(csv)。在数据集中有四列,第一列是艺术家,第二列是专辑,第三列是歌曲名称,第四列是我将曲目scrobbled到last.fm的日期。我已经找到了计算每个艺术家,专辑和歌曲的出现次数的方法,但是我想将这些数据附加到每个数据行,所以我想用一个有7列的csv文件。因此,在每一行中,我想添加歌曲,艺术家和专辑在数据集中的次数。我只是无法弄清楚如何做到这一点。我很难把正确的艺术家赶出柜台。有人可以帮忙吗?

import csv
import collections

artists = collections.Counter()
album = collections.Counter()
song = collections.Counter()
with open('lastfm.csv') as input_file:
   for row in csv.reader(input_file, delimiter=';'):
      artists[row[0]] += 1
      album[row[1]] += 1
      song[row[2]] += 1

    for row in input_file:
      row[4] = artists(row[0])

1 个答案:

答案 0 :(得分:1)

假设输入文件不是很大,你可以再次重复输入文件并用附加的数字写出来,如下所示:

import csv
import collections

artists = collections.Counter()
album = collections.Counter()
song = collections.Counter()
with open('lastfm.csv') as input_file:
    for row in csv.reader(input_file, delimiter=';'):
        artists[row[0]] += 1
        album[row[1]] += 1
        song[row[2]] += 1


with open('output.csv', 'w') as output_file:
    writer = csv.writer(output_file, delimiter=';')
    with open('lastfm.csv', 'r') as input_file:
        for row in csv.reader(input_file, delimiter=';'):
            writer.writerow(row + [song[row[2]], artists[row[0]], album[row[1]]])
相关问题