Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何读取文本文件,使用字典替换字符,然后保存到新文件_Python_Python 3.x_Dictionary - Fatal编程技术网

Python 如何读取文本文件,使用字典替换字符,然后保存到新文件

Python 如何读取文本文件,使用字典替换字符,然后保存到新文件,python,python-3.x,dictionary,Python,Python 3.x,Dictionary,这是学校的作业。我正在使用以下代码: def main(): letters = create_letters() infile = open('note.txt', 'r') text = infile.readlines() infile.close() outfile = open('notetranslate.txt', 'w') outfile.write(text[letters]) outfile.close() def c

这是学校的作业。我正在使用以下代码:

def main():
    letters = create_letters()
    infile = open('note.txt', 'r')
    text = infile.readlines()
    infile.close()
    outfile = open('notetranslate.txt', 'w')
    outfile.write(text[letters])
    outfile.close()

def create_letters():
    return {'A':'`', 'a':'~', "B":'1', 'b':'!', 'C':'2', 'c':'@', 'D':'3', 'd': '#', 'E':'4', 
    'e':'$', 'F':'5', 'f':'%', 'G':'6', 'g':'^', 'H':'7', 'h':'&', 'I':'8', 'i':'*', 'J':'9', 'j':'(', 
    'K':'0', 'k':')', 'L':'-', 'l':'_', 'M':'=', 'm':'+', 'N':'[', 'n':'{', 'O':']', 'o':'}',
    'P':'|', 'p':'/', 'Q':';', 'q':':', 'R':',', 'r':'<', 'S':'.','s':'>','T':'?', 't':'"',
    'U':'`', 'u':'~', 'V':'1', 'v':'!', 'W':'2', 'w':'@', 'X':'3', 'x':'#', 'Y':'4','y':'$', 
    'Z':'5', "z":'%'}
main()
def main():
字母=创建字母()
infle=open('note.txt','r')
text=infle.readlines()
infle.close()
outfile=open('notetranslate.txt','w')
outfile.write(文本[字母])
outfile.close()
def create_letters():
返回{'A':'','A':'~','B':'1','B':'!','C':'2','C':'@','D':'3','D':'#','E':'4',
‘e’:‘元’、‘F’:‘5’、‘F’:‘百分比’、‘G’:‘6’、‘G’:‘^’、‘H’:‘7’、‘H’:‘和’、‘I’:‘8’、‘I’:‘*’、‘J’:‘9’、‘J’:’(’,
‘K’:‘0’、‘K’:’)、‘L’:’-’、‘L’:‘M’:‘=’、‘M’:‘+’、‘N’:‘[’、‘N’:‘{’、‘O’:‘]、‘O’:‘},
‘P’:‘’、‘P’:‘/’、‘Q’:‘’、‘Q’:‘’、‘R’:‘、’、‘R’:‘、‘T’:‘?’、‘T’:‘,’,
‘U’:‘’、‘U’:‘~’、‘V’:‘1’、‘V’:‘!’、‘W’:‘2’、‘W’:‘@’、‘X’:‘3’、‘X’:‘#’、‘Y’:‘4’、‘Y’:‘美元’,
“Z”:“5”,“Z”:“%”
main()
运行这段代码时,我得到一个错误:列表索引必须是整数或片,而不是dict

我的任务是编写一个程序来读取文本文件的内容,然后使用代码字典将文件内容的加密版本写入一个单独的文本文件。第二个文本文件中的每个字符都应该包含文本的加密版本


编写第二个程序(或在当前程序中为用户添加选项菜单),打开加密版本并在屏幕上显示解密文本,供用户阅读。

目的是用
字母中的dict替换字符串

infile = open('note.txt', 'r')
text = infile.read() # read not readlines
infile.close()

subs_text = []
for letter in text:
    subs_text.append(letters[letter]) # lookup the character to be substituted with
subs_text = "".join(subs_text)

outfile = open('notetranslate.txt', 'w+') # use w+ to create file if it does not exist
outfile.write(subs_text)
outfile.close()

为什么
text[字典!]
work?首先想到的是一个字母一个字母地迭代文件的内容,并对每个字母进行替换。还有其他方法。现在试着想想如果文件非常大会发生什么。你在内存中保留了一个巨大的列表,然后又添加了一个巨大的字符串。试着想想这是否可以避免请注意,在某些情况下这可能是一个问题。然而,这是OP问题的一个非常正交的问题。当OP显然是该语言的新手时,将答案的复杂性增加到这样的程度似乎是不合适的。做出这样的更改会给我一个类型错误:字符串索引必须是整数哪个字符串?
subs\u文本?subs\u文本是一个列表。在将所有字符与
合并后,您可能正在使用覆盖的文本。请加入