python填坑系列之jupyter notebook将代码保存为py文件

遇到同样问题的朋友,请直接看下面已经跑通的代码:

%%writefile read_datasets.py
#一、读取达观杯竞赛数据

import pandas as pd
def read_DC_dataset(filename,index):
    dataset=pd.read_csv(filename,header=0,index_col=index)
    return dataset.head()
print(read_DC_dataset('train_set.csv',index='id'))

运行结果如下:

Writing read_datasets.py

jupyter notbook 魔法代码:

%%writefile filename

上面这行代码必须写在代码的最前面一行,否则返回如下错误:

UsageError: Line magic function `%%writefile` not found.
    原文作者:Little_Yuu
    原文地址: https://blog.csdn.net/qq_41072222/article/details/103780945
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞