2014-11-24 67 views
0

我要創建一個基於CSV文件看起來像這樣一本字典創建字典:從文本文件的Python

'song, 2000, 184950' 
'boom, 2009, 83729' 
'boom, 2010, 284500' 
'boom, 2011, 203889' 
'pow, 2000, 385920' 
'pow, 2001, 248930' 
從這個

,我要創建一個包含字的字典鍵,然後將類對象列表作爲值。

這是我迄今爲止...

class Counter(): 
    __slots__ = ('year', 'count') 
    _types = (int, int) 

def readfile(file): 
    d = dict() 
    with open(file) as f: 
     for line in f: 
      element = line.split(,) 
      for word in element: 
       if word in d: 
        d[word].append([Count(int(element[1]), int(element[2]))]) 
       else: 
        d[word] = [Count(int(element[1]), int(element[2]))] 
    print(d) 

我得到的輸出是奇怪的,它是給我一本詞典類似地雷應該是什麼樣子,但它的使用計數(183930 )作爲關鍵字而不是名稱。如果它已經在字典中列出,我還需要它將該類添加到該值。

例如,因爲'繁榮'應該已經在字典中{'boom' : Count(year = 2009, count = 83729)}我希望那裏有一個計數對象列表中的一個值。

預期輸出:

{'song' : [Count(year= 2000, count= 184950)], 'boom' : [Count(year=2009, count=83729), 
Count(year=2010, count= 284500), Count(year=2011, count=203889)], 'pow' : ...etc..} 
+0

@Martijn你確定'''應該在csv例子中?我認爲他們對字符串有意義,但不能作爲示例csv。 – Vyktor 2014-11-24 20:10:57

+0

@Vyktor:OP在他們的樣本中使用了這個;我會留給他們刪除它。 – 2014-11-24 20:12:59

+0

您的代碼無效;它缺少引號(以逗號爲例),而發佈的'Counter()'類將不起作用。你可以給我們*工作代碼*? – 2014-11-24 20:14:50

回答

0

有了這個循環:

for word in element: 
    if word in d: 
     d[word].append([Count(int(element[1]), int(element[2]))]) 
    else: 
     d[word] = [Count(int(element[1]), int(element[2]))] 

你迭代槽上線的所有的話,那麼你打電話(對第一行):

d['song'].append([Count(int('2000'), int('184950'))]) 
d['2000'].append([Count(int('2000'), int('184950'))]) 
d['184950'].append([Count(int('2000'), int('184950'))]) 

只需使用:

for line in f: 
    element = line.split(,) 
    word = element[0] 
    if word in d: 
     d[word].append(Count(int(element[1]), int(element[2]))) 
    else: 
     d[word] = [Count(int(element[1]), int(element[2]))] 

你,如果你使用collections.defaultdict也可以代替你的病情if word in d

import collections 

def readfile(file): 
    d = collections.defaultdict(list) 
    with open(file) as f: 
     for line in f: 
      element = line.split(,) 
      word = element[0] 
      d[word].append(Count(int(element[1]), int(element[2]))) 

    print(d) 
+0

或者您可以使用'd.setdefault(word,[])。append(...)'(使用常規的'dict'而不是'defaultdict')。 – 2014-11-24 20:47:09

+0

@Martijn很好,我不知道這個......但是對我來說似乎有點奇怪(特別是/不是這種情況/如果你有多個分支代碼可以填充字典,你將不得不復制每個地方的默認值)。 – Vyktor 2014-11-24 20:54:34

+0

謝謝!這對我有用! defaultdict對我來說是新的,所以謝謝你給我一種替代方式! @Vyktor – kbaumzie 2014-11-25 15:39:07

0

下面是一些簡單的代碼,做你在找什麼。

from collections import defaultdict 
from pprint import pprint 


class Counter(object): 
    __slots__ = ('year', 'count') 

    def __init__(self, year, count): 
     self.year = year 
     self.count = count 

    def __str__(self): 
     return "Counter(year=%d, count=%d)" % (self.year, self.count) 

    def __repr__(self): 
     return self.__str__() 


def import_counts(): 
    counts = defaultdict(list) 
    with file('data.csv') as f: 
     for line in f: 
      name, year, count = line.split(',') 
      name, year, count = name.strip(), int(year.strip()), int(count.strip()) 
      counts[name].append(Counter(year, count)) 

    return counts 

pprint(import_counts()) 

但是,我將數據格式更改爲正確的CSV,如下所示。

song, 2000, 184950 
boom, 2009, 83729 
boom, 2010, 284500 
boom, 2011, 203889 
pow, 2000, 385920 
pow, 2001, 248930 

產生的輸出是如下:

{ 
    'boom': [ 
     Counter(year=2009, count=83729), 
     Counter(year=2010, count=284500), 
     Counter(year=2011, count=203889) 
    ], 
    'pow': [ 
     Counter(year=2000, count=385920), 
     Counter(year=2001, count=248930) 
    ], 
    'song': [ 
     Counter(year=2000, count=184950) 
    ] 
} 

別注意,如果給定的一個無效的CSV上述不驗證輸入和將錯誤。