常规 Python 中的 %%writefile 魔术命令

我正在将 Python 代码从Jupyter Notebook复制到 .py 文件以从终端运行它。我找不到如何实现这一行的方法:

%%writefile -a submission.py

常规 Python 中与 %%writefile magic 命令相同的代码是什么?


守着一只汪
浏览 105回答 1
1回答

温温酱

替换了这个:%%writefile -a submission.py    model = Net()    model = model.float()    model.load_state_dict(state_dict)    model = model.to('cpu')    model = model.eval()    obs = tensor(obs['board']).reshape(1, 1, config.rows, config.columns).float()    obs = obs / 2    action = model(obs)    return int(action)这样:submission_ending = '''    model = Net()    model = model.float()    model.load_state_dict(state_dict)    model = model.to('cpu')    model = model.eval()    obs = tensor(obs['board']).reshape(1, 1, config.rows, config.columns).float()    obs = obs / 2    action = model(obs)    return int(action)'''with open('submission.py', mode='a') as file:    file.write(submission_ending)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python