将用户输入从一个Python脚本传递到另一个Python脚本。

问题描述 投票:0回答:2

我写了一个脚本,从一个网站导入现有的数据。.csv 文件,修改它,绘制它,还要求用户输入(input2),作为数据集和图形的图形标题和文件名。我希望有另一个脚本(import.py),执行原始脚本(new_file.py),并能够确定用户的输入是什么,这样我就可以访问新创建的文件。如何将用户输入的内容从一个脚本传递到另一个脚本?

接收用户输入的脚本是 new_file.py:

def create_graph():
    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt

    input1 = input("Enter the file you want to import: ")
    data_file = pd.read_excel(input1 + ".xlsx")
    ws = np.array(data_file)

    a = ws[:, 0]
    b = ws[:, 1]
    c = ws[:, 2]
    bc = b + c

    my_data1 = np.vstack((a, b, c, bc))
    my_data1 = my_data1.T

    input2 = input("Enter the name for new graph: ")
    np.savetxt(input2 + ".csv", my_data1, delimiter=',')

    plt.plot(a, b, 'ro')
    plt.plot(a, c, 'go')
    plt.plot(a, bc, 'bo')
    plt.ylabel("y-axis")
    plt.xlabel("x-axis")
    plt.legend(['Column 1 data', 'Column 2 data', 'Column 3 data'], loc='best')
    plt.title(input2)
    plt.savefig(input2)
    plt.show()

第二份剧本(import.py),我试图用它来运行这个目前是。

import new_file as nf

nf.create_graph()

我不知道如何通过 input2new_file.pyimport.py. 谁能帮我一下?谢谢你的帮助

python python-3.x pycharm
2个回答
1
投票

简单地返回值。

def create_graph():
    ...
    return input2

然后在你的其他脚本中。

import new_file as nf

input2 = nf.create_graph()

1
投票

看起来你想做的是返回你的函数中的信息。

def create_graph():
    # ... all of your code other code ...
    return input2

然后在你的import.py里面,你可以像这样接收你的返回值。

import new_file as nf

input2 = nf.create_graph()
# use input2 however you want
© www.soinside.com 2019 - 2024. All rights reserved.