作者wawawa (哇哇哇○( ̄﹏ ̄)○)
看板Python
标题Re: [问题] 请问一下读档的问题
时间Sun Mar 4 01:17:44 2007
※ 引述《isong (船没开啊...)》之铭言:
: 最近在练习读档的问题
: 假如我有一个档 "text.txt"
: 我想要输出的结果为
: ['is','mariamene', 'of', 'offers', 'on', 'one', 'ossuaries',
: 'that', 'tomb']
不只9个吧?你少了一个 the
--
#!/usr/bin/python
import re
result = {}
pattern = re.compile(r'(\w+)')
f = open('text.txt', 'r')
for line in f:
for word in pattern.finditer(line):
key = word.group(1).lower()
if result.has_key(key):
result[key] = result[key] + 1
else:
result[key] = 1
f.close()
print str(result.keys())
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.127.72.61
1F:推 isong:非常谢谢,还要再研究研究 03/04 10:24