0

在下面的节略代码中,我将从函数 OpenFile 中的文件中获取(大量)数据,并且我想在另一个函数 ss 中使用这些数据,而不必再次从文件中读取数据。这可能吗?如果可以,我该怎么做?我认为这相当于在函数之间传递一个变量,但我似乎无法理解这一点或在这种情况下如何应用它。预先感谢您的帮助以及您对我的新手技能的耐心。

<3

from Tkinter import *
from tkFileDialog   import askopenfilename, asksaveasfile

def OpenFile():

    gen = []
    fam = []
    OTUs = []

    save_fam = []
    save_gen = []
    save_OTU = []

    FindIT_name = askopenfilename()
    data = open(FindIT_name, "r").readlines()

    #some data manipulation here


def 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()
4

1 回答 1

1

To be honest, that was too much code for me to really read through, but in general the way you'd want to pass the data between functions would be like this:

def foo(file_path):
    data = open(file_path, 'rb').read()
    result = bar(data)

def bar(data):
    ### do something to data

For applying to your original example something like this would work:

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.
于 2013-06-13T19:24:11.203 回答