2011-01-11 93 views
2

我需要在python中爲列表寫入一個文件。我知道這個列表應該用join方法轉換成字符串,但是因爲我有一個元組,所以我感到困惑。我嘗試了很多改變我的變量爲字符串等,這是我第一次嘗試:python將一個列表寫入文件

def perform(text): 
    repository = [("","")] 
    fdist = nltk.FreqDist(some_variable) 
    for c in some_variable: 
     repository.append((c, fdist[c])) 
    return ' '.join(repository) 

,但它給了我下面的錯誤:

Traceback (most recent call last): 
    File "<pyshell#120>", line 1, in <module> 
    qe = perform(entfile2) 
    File "<pyshell#119>", line 14, in perform 
    return ' '.join(repository) 
TypeError: sequence item 0: expected string, tuple found 

任何想法如何寫列表「存儲庫'到一個文件?謝謝!

+1

你應該更好地解釋什麼是你要回報,這格式的字符串?你想稍後檢索元組嗎? – 2011-01-11 02:08:47

+0

是應該是某種持久性緩存的存儲庫? – 2011-01-11 02:11:09

回答

0

你應該元組的列表使用列表解析首先轉換爲字符串列表,然後以後使用join:

list_of_strings = ["(%s,%s)" % c for c in repository] 
' '.join(list_of_strings) 
1

將它們加入()

之前轉換的元組字符串

我已經相當大幅度重排本,使得:

  1. 現在你的函數是發電機(更低的內存需求)
  2. 傳遞所需的格式 - 它返回任何格式,你要求它返回
  3. 我猜some_variable是可報告的文本子集?

def perform(seq, tell=None, fmt=tuple): 
    """ 
    @param seq: sequence, items to be counted (string counts as sequence of char) 
    @param tell: sequence, items to report on 
    @param fmt: function(item,count) formats output 
    """ 
    # count unique items 
    fdist = nltk.FreqDist(seq) 

    if tell is None: 
     # report on all seen items 
     for item,num in fdist.iteritems(): 
      yield fmt(item,num) 
    else: 
     # report on asked-for items 
     for item in tell: 
      try: 
       yield fmt(item,fdist[item]) 
      except KeyError: 
       # tell contained an item not in seq! 
       yield fmt(item,0) 

# write to output file 
fname = 'c:/mydir/results.txt' 
with open(fname, 'w') as outf: 
    outf.write(' '.join(perform(text, some_variable, ','.join)))   
1

在要存儲在磁盤上的字典的情況下,使用shelve

import shelve 

def get_repository(filename='repository'): 
    # stores it's content on the disk 
    store = shelve.DbfilenameShelf(filename) 

    if not store: 
     # if it's empty fill it 
     print 'creating fdist' 
     # fdist = nltk.FreqDist(some_variable) 
     fdist = dict(hello='There') 
     store.update(fdist) 
    return store 

print get_repository() 
# creating fdist 
# {'hello': 'There'} 
print get_repository() 
# {'hello': 'There'}