私は研究を行い、私の問題の解決策を見つけることができませんでした。基本的には、私のコードは今やっているのと全く同じことをしたいのですが、文章の変数をテキストファイルに置き換えたいと思っています。ここに私の現在のコードされています文字列変数の代わりにテキストファイルを使用しようとしています
from collections import OrderedDict
sentence= ("I met a traveller from an antique land, Who said Two vast and trunkless legs of stone. Stand in the desert. . . . Near them, on the sand, Half sunk a shattered visage lies, whose frown, And wrinkled lip, and sneer of cold command, Tell that its sculptor well those passions read Which yet survive, stamped on these lifeless things, The hand that mocked them, and the heart that fed; And on the pedestal, these words appear: My name is Ozymandias, King of Kings; Look on my Works, ye Mighty, and despair! Nothing beside remains. Round the decay Of that colossal Wreck, boundless and bare The lone and level sands stretch far away.").lower()
words = sentence.split(' ')
lst = list(OrderedDict.fromkeys(words))
numberLst = []
for i in words:
numberLst.append(lst.index(i)+1)
words_str = ':'.join(words)
numberLst_str = ':'.join(str(e) for e in numberLst)
file = open("words.txt","w")
file.write(words_str)
file.close()
file=open("numberlst.txt","w")
file.write(numberLst_str)
file.close()
joinlst = " ".join(lst[i-1] for i in numberLst)
file=open("joinlst.txt","w")
file.write(joinlst)
file.close()
choice = input ("do you want to uncompress or compress the text file (type compress or uncompress)")
if choice == "compress":
print (numberLst)
else:
if choice == "uncompress":
print (joinlst)
print("your choice was",choice)
歓声m8cheers m8cheers m8cheers m8cheers m8cheers m8cheers m8cheers m8cheers m8cheers m8cheers m8cheers m8cheers m8cheers m8cheers m8cheers m8cheers m8cheers m8cheers m8cheers m8cheers m8cheers m8cheers m8cheers m8cheers M8 – alibongo
@alibongo私は助けることができるうれしいです!投稿を投票して、それがうまくいけば答えとして選択してください:) – Treebasher