简体   繁体   English

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

[英]Python: appending a counter to a csv file

I am working on a project with data(csv) i gathered from last.fm. 我正在使用从last.fm收集的data(csv)进行项目。 In the dataset there are four columns, the first is the artist, second the album, the 3th is the songname and the fourth is the date at which i scrobbled the track to last.fm. 在数据集中有四列,第一列是艺术家,第二列是专辑,第三列是歌曲名,第四列是我将曲目划到last.fm的日期。 I already have found a way of counting the number of occurences of each artist, album and song, but i would like to append this data to each data row so i would and up with an csv file that has 7 columns. 我已经找到一种计算每个艺术家,专辑和歌曲的出现次数的方法,但是我想将此数据附加到每个数据行,因此我将使用具有7列的csv文件。 So in each row i want to add the number of times that the song, artist and album are in the dataset. 因此,我想在每一行中添加歌曲,艺术家和专辑在数据集中的次数。 I just cannot figure out how to do this. 我只是不知道如何做到这一点。 I have a hard time to get the right artist out of the counter. 我很难找到合适的艺术家。 Can someone help? 有人可以帮忙吗?

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])

Assuming that the input file isn't enormous, you can just reiterate over your input file a second time and write the lines out with the counts appended, like so: 假设输入文件不是很大,您可以再次重复输入文件,并写出行并附加计数,如下所示:

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]]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM