2013-04-27 37 views
0

我正在嘗試創建文本集合的雙峯圖,以便我可以通過文本或單詞通過文本來投影任一文本的網絡。我的一位同事表示,如果我能得到如下格式的一個CSV文件中的所有我的文件,然後有一個工作流程,將剩下的工作:Python:將目錄中的所有文件寫入一個cdv文件

textfile1, words words words 
textfile2, words words words 

我寫了下面的腳本:

#! /usr/bin/env python 

# a script to convert all text files in a directory to the format: 
# filename, words from file (no punctuation) 

import glob 
import re 

files = {} 
for fpath in glob.glob("*.txt"): 
    with open(fpath) as f: 
     just_words = re.sub("[^a-zA-Z'-]"," ",f.read()) 

with open("mastertext.csv", "w") as f: 
    for fname in files: 
     print >> f , "%s,%s"%(fname,just_words) 

該腳本將運行併產生輸出文件,但輸出文件是空白,我沒有得到任何錯誤響應 - 多學習對我來說是源作爲一個Python新手。我在這裏的正確軌道,如果是這樣,我錯過了什麼?

回答

1

您需要將數據保存到just_wordsfiles。在這種情況下,我使用元組列表而不是字典,但如果您願意,仍可以使用字典。 :-)

files = [] 
for fpath in glob.glob("*.txt"): 
    with open(fpath) as f: 
     just_words = re.sub("[^a-zA-Z'-]"," ",f.read()) 
     files.append((fpath, just_words)) 

with open("mastertext.csv", "w") as f: 
    for fname, just_words in files: 
     print >> f , "%s,%s"%(fname,just_words) 
+0

這很有效,但我明顯不明白我在做什麼,因爲我認爲我是在'用open ...'而不是在上面的代碼塊中創建結果。當然,我希望在Python中有一個朗讀函數,它可以告訴你一個塊中的每一行實際上在做什麼。感謝您的幫助和耐心! – 2013-04-27 20:49:02

相關問題