在Python編碼麻煩我有編碼的,請幫我解決這個問題的問題我遇到解決
問題:
我的Python代碼應導入輸入文件中找出的頻率在它應該輸入文件中的每個詞出現在文件中,並在按字母順序的字寫入到稱爲輸出文件word_frequency.txt
代碼:
import string
def itm((a1,b1), (a2,b2)):
if b1 > b2:
return - 1
elif b1 == b2:
return cmp(a1, a2)
else:
return 1
def main_cd():
input.txt = raw_input("File to analyze: ")
text = open(input.txt, 'r').read()
text = string.lower(text)
for i in string.punctuation:
text = string.replace(text, i, ' ')
words = string.split(text)
counts = {}
for w in words:
counts[w] = counts.get(w,0) + 1
n = input("Output analysis of how many words? ")
items = counts.items()
items.sort(itm)
for i in range(n):
print "%-10s%5d" % items[i]
if __name__ == '__main__': main_cd()
問題:
文件來分析:input.txt中 回溯(最近通話最後一個): 線38,在 如果名 == '主要':main_cd() 線19,在main_cd input.txt中的raw_input =( 「文件來分析:」) AttributeError的: 'builtin_function_or_method' 對象沒有屬性 'TXT'
過程,退出代碼完成1
不叫變量'input.txt'。只要'infile'或'input'就可以了。 – TyrantWave
也是,'main_cd()'與'if'在同一行,它需要縮進,因爲我已經更新了問題。 – Amit
@TyrantWave'input'也不好。它會掩蓋內置函數 – cmd