我编写了一个脚本,该脚本从.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()
我不确定如何将input2
的{{1}}传递到new_file.py
。谁能帮我吗?谢谢
答案 0 :(得分:1)
简单地返回值。
def create_graph():
...
return input2
然后使用其他脚本:
import new_file as nf
input2 = nf.create_graph()
答案 1 :(得分: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