2017-02-25 61 views
0
from collections import Counter 
from glob import iglob 

import re 
import os 

def remove_garbage(text): 
    """Replace non-word (non-alphanumeric) chars in text with spaces, 
     then convert and return a lowercase version of the result. 
    """ 
    text = re.sub(r'\W+', ' ', text) 
    text = text.lower() 
    return text 

topwords = 50000 
folderpath = 'd:/jktextall/' 
counter = Counter() 

for filepath in iglob(os.path.join(folderpath, '*.txt')): 
    with open(filepath) as file: 
     counter.update(remove_garbage(file.read()).split()) 
file1 = open("jkwords1.txt","w") 
for word, count in counter.most_common(topwords): 
file1.write (str(count) +"," + word + "\n") 

file1.close 

我修改了上面的代码,将输出写入文本文件jkwords1.txt。没有任何内容正在写入文本文件。但是,print (word,count)的确在python控制台中产生输出。Python循环的输出不会去文本文件

但是,如果使用print(count,word),则不使用file1.write,而是使用屏幕输出。

+0

使用'开放的(...),因为文件1:...',以确保一切都被写入磁盘正常。 –

回答

0

您没有正确地拨打close功能。

file1.close - >file1.close()

+0

是否做到了。在jkwords1.txt文件中只有一个记录与之前没有记录(在使用file1.close()之前)。但是,print关键字会导致较大的输出。 – user1955215

+0

你是否介意修复缩进问题,'file1.write(str(count)+“,”+ word +“\ n”)'似乎没有正确缩进。 –

+0

谢谢。缩进的变化有所帮助。对Python来说是新手。需要了解更多。 – user1955215