新手入坑。
通常我們需要把公共函數(shù)提出來,作為公共資源調(diào)用。也避免了代碼的重復書寫。
比如我們在項目內(nèi)創(chuàng)建我們的py腳本路徑如下:
在公共方法中定義方法:文章來源:http://www.zghlxwxcb.cn/news/detail-633102.html
class CommonMethods:
def dataFormat(df):
dataList = []
for row in range(0, df.shape[0]):
dataList.append({})
for col in range(0, df.shape[1]):
col_name = df.columns.values[col]
dataList[row][col_name] = df.loc[row, col_name]
return dataList
在其他腳本中調(diào)用如下:文章來源地址http://www.zghlxwxcb.cn/news/detail-633102.html
import akshare as ak
from Common.CommonMethod import CommonMethods //from 文件路徑 import自己定義的類
stock_board_change_em_df = ak.stock_board_change_em()
data=CommonMethods.dataFormat(stock_board_change_em_df)//此處直接調(diào)用即可
print(str(data))
到了這里,關于Python 調(diào)用自定義函數(shù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!