如何使用CSV文件在Python中创建动态词典

时间:2016-03-23 03:46:24

标签: python python-2.7 csv dictionary append

问题很简单,我有一个包含四列的CSV文件,我想要对第一列进行赋值,并将其放入我的python脚本中的字典中。我不想在完成任务的字典日期中添加值。

此数据的CSV数据位于名为VC.csv的文件中,例如:

24M Technologies,Series A,8/19/10
24M Technologies,Grant,8/16/10
2B Energy,Private Equity,3/18/14
2B Energy,Series B,3/18/14
2B Energy,Unattributed VC,5/1/08
3GSolar Photovoltaics,Series A,12/17/12
3sun Group,Growth Equity,3/3/14
3Tier Group,Series C,11/17/08

我想要的最终结果是打印时的字典。

例如

>>> print 3TierGroup
>>>
>>>{'company': '2B Energy', 'Private Equity': '3/18/14', 'Series B': '3/18/14', 'Unattributed VC': '05/01/08'}

我的问题在于尝试循环播放并向已定义的字典添加更多内容。而不是追加我猜测我正在重新创建并覆盖每次传递的循环。我得到的结果是{'company': '2B Energy', 'Private Equity': '3/18/14'}我需要我的代码的最后一行来测试字典是否已经存在;如果是这样,它会附加额外的圆形日期。

这是我的代码......

import csv

companyList =[]
transactionDates=[]
dictNames=[]

def fileNameCleaner(namer):
    namer = namer.replace(' ', '')
    namer = namer.replace(',','')
    namer = namer.replace('-','')
    namer = namer.replace('.','')
    namer = namer.replace('_','')
    namer = namer.replace('@','')
    namer = namer.replace('&','')
    return namer

with open('VC.csv', 'rb') as rawData:
    timelineData = csv.reader(rawData, delimiter=',', quotechar='"')      # Open CSV file and snag data
    for line in timelineData:  # Run through each row in csv
        companyList.append(fileNameCleaner(line[0])) # Create list and remove some special charcters
    companyList = list(set(companyList))    # Remove duplicates and Sort

for companyListRow in companyList:
    with open('VC.csv', 'rb') as rawDataTwo:
        timelineDataTwo = csv.reader(rawDataTwo, delimiter=',', quotechar='"')
        for TList in timelineDataTwo:
            company = TList[0]
            finRound = TList[1]
            tranDate = TList[2]
            if companyListRow == fileNameCleaner(TList[0]):
                companyListRow = {'company':TList[0], finRound:tranDate }
                print companyListRow

2 个答案:

答案 0 :(得分:0)

我认为此代码将总结您的公司数据,只需一次通过CSV:

# define dict (to be keyed by company name) to accumulate company attributes from CSV file
company_data = {}

with open('VC.csv', 'rb') as rawData:
    # Open CSV file and snag data
    timelineData = csv.DictReader(rawData, delimiter=',', quotechar='"',
                                  fieldnames=['company','key','value'])

    # Run through each row in csv
    for line in timelineData:  
        name = filenameCleaner(line['company'])
        # get record for previously seen company, or get a new one with just the name in it
        rec = company_data.get(name, {'company': name})

        # add this line's key-value to the rec for this company
        rec[line['key']] = line['value']

        # stuff updated rec back into the overall summarizing dict
        company_data[name] = rec

# now get the assembled records by getting just the values from the summarizing dict
company_recs = company_data.values()

答案 1 :(得分:0)

阅读您的数据:

str1='''24M Technologies,Series A,8/19/10
24M Technologies,Grant,8/16/10
2B Energy,Private Equity,3/18/14
2B Energy,Series B,3/18/14
2B Energy,Unattributed VC,5/1/08
3GSolar Photovoltaics,Series A,12/17/12
3sun Group,Growth Equity,3/3/14
3Tier Group,Series C,11/17/08'''

list1= str1.split('\n')
print list1

我认为您只需要一个字典(不是很多),因此您可以按公司名称查找数据,例如像这样:

comps={}
for abc in list1:
    a,b,c=abc.split(',')
    if not a in comps:
        comps[a]= [[b,c]]
    else:
        comps[a].append([b,c])

for k,v in comps.iteritems():
    print k,v

输出:

3sun Group [['Growth Equity', '3/3/14']]
2B Energy [['Private Equity', '3/18/14'], ['Series B', '3/18/14'], ['Unattributed VC', '5/1/08']]
3Tier Group [['Series C', '11/17/08']]
3GSolar Photovoltaics [['Series A', '12/17/12']]
24M Technologies [['Series A', '8/19/10'], ['Grant', '8/16/10']]

您的词典条目的值将是一个事件列表,每个事件都是一个列表,首先是类型,然后是日期。