2015-11-20 19 views
0

這是我到目前爲止有:寫入鍵,用於分隔文件夾中文本文件的值?

for key,value in spee_dict: 
    with open(key, 'w+') as f: 
     f.write(value) 

我得到如下:ValueError: too many values to unpack

也許這是因爲我有存儲爲每個鍵值的話數萬。有893個鍵。我怎樣才能解決這個錯誤?

編輯:

Keyvalue都是字符串。

這裏是鍵和值的幾個例子爲spee_dict

key  value 
speech1 we should have gone to the other country a few years ago 
speech2 our tax situation is completely fine and revenues are increasing 
speech3 ladies and gentlemen, thank you for your attention 
... 

基本上,我想在我的ü的文件夾中:/驅動器文件,如speech1.txtspeech2.txtspeech3.txt

回答

1

字典迭代僅在按鍵上。

而是使用

for key,value in spee_dict.items(): 
    with open(key, 'w+') as f: 
     f.write(value) 

如果你是在Python 2中,這將是有意義的使用iteritems代替items(即它不會產生一個名單,但發電機)

for key,value in spee_dict.iteritems(): 
    with open(key, 'w+') as f: 
     f.write(value) 
+0

啊對。由於某種原因,這不會寫入我的文件夾在目錄:'U:/ wordfolder' ..它只是在控制檯上運行 – blacksite

+1

我相信OP想要將每個值寫入其自己的文本文件,並使用相應密鑰的文件名。 – pzp

+1

@pzp是的謝謝,我現在才注意到......這是通常的[XY問題](http://xyproblem.info/)咬 – Pynchia

相關問題