def gettext():
txt = open("C:/Users/asus/Desktop/englishtalk.txt","rb").read()
txt = txt.lower()
for ch in '''!",':.''':
txt = txt.replace(ch,b" ")
return txt
englishtalktxt=gettext()
words = englishtalktxt.split()
counts ={}
for word in words:
counts[word]=counts.get(word,0)+1
items = list(count.item())
items.sort(key=lambda x:x[1],reverse=True)
for i in range(10):
word,count =items[i]
print("{0:<10}{1:>5}".format(word,count))
最后得出来
Traceback (most recent call last):
File "C:/Users/asus/Desktop/python/english.py", line 7, in <module>
englishtalktxt=gettext()
File "C:/Users/asus/Desktop/python/english.py", line 5, in gettext
txt = txt.replace(ch,b" ")
TypeError: a bytes-like object is required, not 'str'
>>>
这是怎么回事啊,网上说的我搞了还是没用
txt = open("C:/Users/asus/Desktop/englishtalk.txt","rb").read()
txt = txt.lower()
for ch in '''!",':.''':
txt = txt.replace(ch,b" ")
return txt
englishtalktxt=gettext()
words = englishtalktxt.split()
counts ={}
for word in words:
counts[word]=counts.get(word,0)+1
items = list(count.item())
items.sort(key=lambda x:x[1],reverse=True)
for i in range(10):
word,count =items[i]
print("{0:<10}{1:>5}".format(word,count))
最后得出来
Traceback (most recent call last):
File "C:/Users/asus/Desktop/python/english.py", line 7, in <module>
englishtalktxt=gettext()
File "C:/Users/asus/Desktop/python/english.py", line 5, in gettext
txt = txt.replace(ch,b" ")
TypeError: a bytes-like object is required, not 'str'
>>>
这是怎么回事啊,网上说的我搞了还是没用