嘗試洗掉本地 outputtext.txt 檔案中的所有引號和大寫字母實體
此代碼基于此網站 https://www.thepythoncode.com/article/text-generation-keras-python
sequence_length = 100
BATCH_SIZE = 128
EPOCHS = 30
text = open("outputtext.txt",'r',encoding="utf-8")
text = text.lower()
text = text.translate(str.maketrans("", "", punctuation))
但是,當我運行代碼時;lower 函式和 translate 函式都回傳錯誤
lower:AttributeError: '_io.TextIOWrapper' 物件沒有屬性 'lower'
翻譯:AttributeError:“_io.TextIOWrapper”物件沒有屬性“翻譯”。您是說:“截斷”嗎?
我已經嘗試過使用讀寫權限,但這似乎不起作用?
uj5u.com熱心網友回復:
檔案內容:
Sam" goes "To" the StOre.
讀入檔案、編輯檔案、列印檔案內容
with open('outputtext.txt', 'r') as f:
lines = f.readlines()
lines = lines[0].lower().replace('"','')
# output: sam goes to the store.
print(lines)
讀入檔案,編輯檔案,將行寫入新檔案
with open('outputtext.txt', 'r') as infile:
lines = infile.readlines()
lines = lines[0].lower().replace('"','')
with open('new_outputtext.txt', 'w') as outfile:
lines = outfile.write(lines)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/470761.html
