为什么我一直这样
AttributeError: 'dict_keys' object has no attribute 'sort'
Run Code Online (Sandbox Code Playgroud)
还是我的代码?我怎样才能解决这个问题?
import string
infile = open('alice_in_wonderland.txt', 'r')
text = infile.readlines()
counts = {}
for line in text:
for word in line:
counts[word] = counts.get (word, 0) +1
'''
if word != " ":
if word != ".":
'''
word_keys = counts.keys()
word_keys.sort()
infile.close()
outfile = open("alice_words.txt", 'w')
outfile.write("Word \t \t Count \n")
outfile.write("======================= \n")
for word in word_keys:
outfile.write("%-12s%d\n" % (word.lower(), counts[word]))
outfile.close()
Run Code Online (Sandbox Code Playgroud)
我不知道还能做什么.
我想替换字符串中的字符,不使用字符串函数或类似string.replace.
>>> replace ("banana", "a", "e")
'benene'
Run Code Online (Sandbox Code Playgroud)
因此,对于该示例,我想在字符串"banana"中将字符"a"替换为"e"