如何使用python更改ipynb文件中的文本

时间:2018-12-13 09:04:53

标签: python jupyter-notebook

我在ipynb文件中有密码作为变量。

如何在python的帮助下以编程方式为新密码更改此密码?

我已经尝试了以下方法,但是没有用:

with open("file.ipynb", "r") as initial_file:
    text = initial_file.read()
    text.replace('passwordone', 'passwordtwo')
with open("file.ipynb", "w") as new_file:
    new_file.write(text)

1 个答案:

答案 0 :(得分:1)

将文本变量分配为新值

with open("file.ipynb", "r") as initial_file:
    text = initial_file.read()
    text = text.replace('passwordone', 'passwordtwo')

with open("file.ipynb", "w") as new_file:
        new_file.write(text)