簡體   English   中英

如何在 Python 參數中傳遞 Ruby 實例變量

[英]How to pass Ruby instance variable in a Python argument

我正在構建一個 Rails 應用程序,它接收在 Python 中處理的 CSV 文件。 我正在做一些簡單的測試並設法在我的 Rails 應用程序中執行一個 Python 腳本並將結果輸出到瀏覽器中。

現在,我想傳遞上傳到 rails 應用程序的 CSV 文件,作為我的 Python 腳本的參數。

這是我目前嘗試過的:

upload_excel_controller.rb

require 'json'

class UploadExcelController < ApplicationController
    require 'csv'

    def index
    end

    def import
      rowarray = Array.new
      myfile = params[:file]

      @rowarraydisp = CSV.read(myfile.path)
      @result = JSON.parse(`python lib/assets/python_scripts/python_test.py @rowarraydisp`)
    end
end

python_test.py

import json, sys

arg = sys.argv[1]

def main():
    return(arg)

if __name__ == "__main__":
    json.dump(main(), sys.stdout)

index.html.erb

<%= form_tag({:action => :import}, multipart: true) do %>
    <%= file_field_tag :file %>
    <%= submit_tag( "Import" ) %>
<% end %>

導入.html.erb

<% @result.each do |substring| %>
    <%= substring %>
    <p>End of this string, check next line for the other one!
<% end %>

<p>End of script</p>

當我運行這個時,我得到這個錯誤: undefined method 'each' for "@rowarraydisp":String

似乎@rowarraydisp是作為字符串傳遞的。

像這樣嘗試

%x[python lib/assets/python_scripts/python_test.py #{@rowarraydisp}]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM