[英]Python: appending a counter to a csv file
我正在使用从last.fm收集的data(csv)进行项目。 在数据集中有四列,第一列是艺术家,第二列是专辑,第三列是歌曲名,第四列是我将曲目划到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])
假设输入文件不是很大,您可以再次重复输入文件,并写出行并附加计数,如下所示:
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.