如何通过从Deno导入运行Python脚本?

2024-05-16 13:08:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在尝试使用以下代码从deno后端运行python文件

const cmd = Deno.run({
    cmd: ["python", "python.py"], 
    stdout: "piped",
    stderr: "piped"
});

const output = await cmd.output() // "piped" must be set
const outStr = new TextDecoder().decode(output);

const error = await cmd.stderrOutput();
const errorStr = new TextDecoder().decode(error);

cmd.close(); 
console.log(outStr, errorStr);

const resultsAlgorithm = outStr
console.log('This is a test, python result is...',outStr)
console.log('Finished')

该代码适用于“print(“Hello”)”等基本脚本,但无法在更复杂的脚本上运行导入,例如

   import pandas as pd  # Changes call up name to pd
   from yahoofinancials import YahooFinancials
   from datetime import date, datetime, timedelta,time

   Yahoo_Forex = pd.DataFrame()
   Currency_Pair_Prices = pd.DataFrame()

   print('Running')


    def DataExtract(FileName, DataFrameName, DataFrameName_2, time_range, Interval, ColumnName):
        print('Function started')
        start = date.today() - timedelta(days=2)  
        end = date.today() - timedelta(days=time_range)  
        DataFrameName = pd.read_excel(FileName, header=None)  
        DataFrameName.columns = ColumnName  
        n = 0  
        for ticker in DataFrameName[ColumnName[0]]:  
            Currency_Pair = DataFrameName.iloc[n, 1]  
            Currency_Pair_Ticker = YahooFinancials(ticker)  
            data = Currency_Pair_Ticker.get_historical_price_data(
                str(end), str(start), Interval) 
            Extracted_Data = pd.DataFrame(data[ticker]["prices"])  
            Currency_Close_Price = (Extracted_Data["close"])  
            DataFrameName_2[str(Currency_Pair)] = Currency_Close_Price  
            n = n+1  # 13
        print(DataFrameName_2)
        print("DataExtract Completed")
    
    
    DataExtract("yahoo_Forex.xlsx", Yahoo_Forex, Currency_Pair_Prices,int(10), "daily", ["Ticker", "Pair", "Exchange"])

python代码本身就可以成功运行,因此必须使用deno,但请确定我需要更改什么,以便获得任何帮助,我们将不胜感激


Tags: 代码importcmdlogoutputdatecurrencypiped