我试图用python解决一个元组问题,我在终端上运行python代码,但是每次运行它时,它都会把我存储的文件夹带回来,没有任何输出。代码如下:
name = raw_input("Enter file:")
if len(name) < 1 :
name = "mbox-short.txt"
handle = open(name)
count = dict()
fh = handle.read()
for line in fh :
lines = line.rstrip()
if lines.startswith('From '):
word = lines.split()``
words = word[5]
wordss = words.split()
wordsss = wordss[0]
for letters in wordsss :
count[letters] = wordsss.get(letter, 0) +1
lst = list ()
for k,v in count.items() :
lst.append( (k,v) )
lst.sort(k)
print lst
在这些行中,您读取所有文件内容并将其存储在
fh
中作为string
。现在,当您在for line in fh
中对它进行迭代时,您只得到line
中的单个字符,因此line.rstrip()
没有太大意义,if lines.startswith('From ')
输出:
请看这个:http://www.pythonlearn.com/html-007/cfbook011.html
相关问题 更多 >
编程相关推荐