您现在的位置是:主页 > news > 承德 网站建设/seo排名优化关键词
承德 网站建设/seo排名优化关键词
admin2025/5/29 9:36:55【news】
简介承德 网站建设,seo排名优化关键词,男生专属浏览器,怎么创建公众号写文章我正在试着运行一个程序(word_count.py)在windows中的命令行中。但是,它在读取文件时抛出和异常。有人能告诉我为什么吗?在这是py文件:import sysimport reimport stringdef Usage():print "Usage:word_count.py [doc1] [doc2] ... [doc…
我正在试着运行一个程序(word_count.py)在windows中的命令行中。
但是,它在读取文件时抛出和异常。有人能告诉我为什么吗?在
这是py文件:import sys
import re
import string
def Usage():
print "Usage:word_count.py [doc1] [doc2] ... [docN]"
if len(sys.argv) == 1:
Usage()
sys.exit()
for fn in sys.argv[1:]:
try:
with open(fn) as textf:
word_map = {}
total_cnt = 0
# reads one line at a time
for line in textf:
line = line.strip()
if not line:
continue
tempwords = line.split()
for w in tempwords:
w = re.sub('[%s]' % re.escape(string.punctuation), '', w)
if w:
if w.lower() in word_map:
word_map[w.lower()] += 1
else:
word_map[w.lower()] = 1
total_cnt += 1
print fn+' ------ total word count: '+str(total_cnt)
output_f_name = 'word_count_'+fn
with open(output_f_name, 'wb') as output:
for ele in sorted(word_map.items(), key=lambda x:x[1], reverse=True):
output.write('{} {}\n'.format(str(ele[1]).rjust(6), ele[0].ljust(2)))
except IOError:
print 'Cannot open file %s for reading' % fn
exit(1)
.py文件和.txt文件都位于我的桌面上,我使用以下命令从命令行运行程序:
c:\Users\Me\Desktop>;word_count.py[文件.txt]在