[英]opening a text file in python and reading the contains as str
我正在嘗試使用RSA算法在python中加密文件,因為我已經存儲了可以在普通文本文件中寫入的所有可能值。 像這樣
SYMBOLS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 !?.'
現在我有一個要加密的文本文件。 它僅包含一行; “我是男孩”(未引用這些內容)。 但是,當我嘗試加密文件時,該文件顯示:“ SYMBOLS沒有字符”,這是一條消息,如果字符不匹配,則程序將傳遞該消息。 這是我聲明符號並打開文本文件的代碼:
SYMBOLS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 !?'
def main():
# Runs a test that encrypts a message to a file or decrypts a message
# from a file.
filename = 'encrypted_file.txt' # The file to write to/read from.
mode = 'encrypt' # Set to either 'encrypt' or 'decrypt'.
if mode == 'encrypt':
message1 = open('afile.txt', 'r') #open the file which will be encrypted
message = str((message1.read())
print(message)
我認為我在打開文本文件時犯了一個錯誤,因為對於此腳本,只需要將包含作為str,但是我不知道該怎么做。 期待一些建議。 謝謝。
請嘗試以下代碼,然后再執行以下代碼以從文件中讀取內容。 錯誤消息似乎與代碼無關,因為您未在任何地方使用SYMBOLS。
在message = str(message1.read())中,您沒有匹配的左括號和右括號。
SYMBOLS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 !?'
def main():
# Runs a test that encrypts a message to a file or decrypts a message
# from a file.
filename = 'encrypted_file.txt' # The file to write to/read from.
mode = 'encrypt' # Set to either 'encrypt' or 'decrypt'.
if mode == 'encrypt':
message1 = open(filename, 'r') #open the file which will be encrypted
message = str(message1.read())
print(message)
main()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.