【问题标题】:Extract information from multiple CSV files, write to new CSV with third column从多个 CSV 文件中提取信息,使用第三列写入新的 CSV
【发布时间】:2014-11-18 05:21:56
【问题描述】:

我有一个包含四个 CSV 文件的文件夹。在每个 CSV 中都有动物,并且每种动物出现的次数。我正在尝试创建一个 CSV,该 CSV 从文件夹中的所有 CSV 收集信息,删除重复项,并添加第三列,列出找到动物的原始文件。例如 lion,4,'file2, file4'

我真的希望我的新 CSV 有第三列,列出包含每种动物的文件,但我想不通。我试着用第二本字典来做——参考locationCount的行。 在下面查看我正在使用的当前脚本。

我拥有的文件:

file1.csv:
cat,1
dog,2
bird,1
rat,3

file2.csv:
bear,1
lion,1
goat,1
pig,1

file3.csv:
rat,1
bear,1
mouse,1
cat,1

file4.csv:
elephant,1
tiger,2
dog,1
lion,3

当前脚本:

import glob
import os
import csv, pdb

listCSV = glob.glob('*.csv')
masterCount = {}
locationCount = {}
for i in listCSV: # iterate over each csv
    filename = os.path.split(i)[1] # filename for each csv
    with open(i, 'rb') as f:
        reader = csv.reader(f)
        location = []
        for row in reader:
            key = row[0]
            location.append(filename)
            masterCount[key] = masterCount.get(key, 0) + int(row[1]) 
            locationCount[key] = locationCount.get(key, location)
writer = csv.writer(open('MasterAnimalCount.csv', 'wb'))
for key, value in masterCount.items():
    writer.writerow([key, value])

【问题讨论】:

    标签: python csv


    【解决方案1】:

    您几乎是对的 - 以与处理计数相同的方式处理位置。

    我已经重命名并改组了一些东西,但它基本上是相同的代码结构。 masterCount 将一个数字添加到之前的数字中,masterLocations 将一个文件名添加到之前的文件名列表中。

    from glob import glob
    import os, csv, pdb
    
    masterCount = {}
    masterLocations = {}
    
    for i in glob('*.csv'):
        filename = os.path.split(i)[1]
    
        for animal, count in csv.reader(open(i)):
            masterCount[animal] = masterCount.get(animal, 0) + int(count) 
            masterLocations[animal] = masterLocations.get(animal, []) + [filename]
    
    writer = csv.writer(open('MasterAnimalCount.csv', 'wb'))
    
    for animal in masterCount.keys():
        writer.writerow([animal, masterCount[animal], ', '.join(masterLocations[animal])])
    

    【讨论】:

    • 非常感谢!我为此工作了几个小时,但没有取得多大成功。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 2022-12-24
    • 2013-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多