在下面的简化代码中,我将从功能OpenFile中的文件中获取(大量)数据,并且我想在另一个功能ss中使用该数据,而不必再次从该文件中读取数据。这可能吗,如果可以,我该怎么做?我认为这等效于在函数之间传递变量,但是我似乎无法解决这个问题或在这种情况下如何应用它。预先感谢您的协助以及您对我的新手技能的耐心配合。<3from Tkinter import *from tkFileDialog import askopenfilename, asksaveasfiledef OpenFile(): gen = [] fam = [] OTUs = [] save_fam = [] save_gen = [] save_OTU = [] FindIT_name = askopenfilename() data = open(FindIT_name, "r").readlines() #some data manipulation heredef ss(): ss_file = asksaveasfile(mode="w", defaultextension=".csv") ss_file.write("OTU, Family, Genus") #I want to get data here, specifically data from FindIT_name (see OpenFile function) root = Tk()root.minsize(500,500)root.geometry("500x500")root.wm_title("Curate Digitized Names")menubar = Menu(root)filemenu = Menu(menubar, tearoff=0)filemenu.add_command(label="Get FindIt Input", command=OpenFile)filemenu.add_separator()filemenu.add_command(label="Quit", command=stop)menubar.add_cascade(label="File", menu=filemenu)root.config(menu=menubar)root.mainloop()
1 回答
繁华开满天机
TA贡献1816条经验 获得超4个赞
老实说,这对于我来说真的是太多代码了,但通常来说,您想要在函数之间传递数据的方式是这样的:
def foo(file_path):
data = open(file_path, 'rb').read()
result = bar(data)
def bar(data):
### do something to data
对于您的原始示例,可以使用以下方法:
def OpenFile():
gen = []
fam = []
OTUs = []
save_fam = []
save_gen = []
save_OTU = []
FindIT_name = askopenfilename()
ss(FindIT_name)
data = open(FindIT_name, "r").readlines()
#some data manipulation here
def ss(FindIT_name):
ss_file = asksaveasfile(mode="w", defaultextension=".csv")
ss_file.write("OTU, Family, Genus")
### If you need to do something, you now have FindIT_name available.
添加回答
举报
0/150
提交
取消