[英]How do I use data from one function in another function in python?
Hello I am pretty new to python and I want to do the following:你好,我对 python 很陌生,我想做以下事情:
I have a function that opens a file, reads the file, closes the file and returns the data:我有一个打开文件、读取文件、关闭文件并返回数据的函数:
def getFastaFromFile(filename):
""" Read a fasta file (filename) from disk and return
its full contents as a string"""
inf=open(filename)
data=inf.read()
inf.close()
return data
The data that is being returned are a few lines with strings.返回的数据是带有字符串的几行。
What I want to do is have another function that uses the data from the first function and perform the .readlines()
, .readline()
and .count()
commands我想要做的是使用第一个函数中的数据并执行
.readlines()
、 .readline()
和.count()
命令的另一个函数
My second function:我的第二个功能:
def printTableFromFasta(fastarec):
a= data.readlines()
for i in range(a)
b= data.readline()
c= b.count('A')
print(c)
As output I would like to print the amount of times string "A" appears for every line from the data.作为输出,我想打印数据中每一行出现字符串“A”的次数。 The problem I get with this code is that the data doesn't get recognized.
我使用此代码遇到的问题是无法识别数据。
First, you need to pass the data you are wanting to read into the second function, like so首先,您需要将要读取的数据传递到第二个函数中,如下所示
def printTableFromFasta(data):
In order to get this from your first function, try returning the entire contents of the file为了从你的第一个函数中得到这个,尝试返回文件的全部内容
def getFastaFromFile(filename):
with open(filename, 'r') as inf: # handles open and close
data = inf.readlines() # Returns the entire file as a list of strings
return data
Your function call will look something like this你的函数调用看起来像这样
printTableFromFasta(getFastaFromFile(filename))
Then, in your second function, you don't need to call readlines
, it's already a list.然后,在你的第二个函数中,你不需要调用
readlines
,它已经是一个列表。
def printTableFromFasta(data):
for line in data # look at each line
print(line.count('A')) # count 'A'
Edit: To only read from the second function and not touch the first function
编辑:只从第二个功能中读取而不是触摸第一个功能
def printTableFromFasta(filename):
with open(filename, 'r') as inf: # handles open and close
for line in inf.readlines() # look at each line
print(line.count('A')) # count 'A'
Remember that the data
variable in the first function is local .请记住,第一个函数中的
data
变量是local 。 It cannot be accessed from outside the function it is defined in.不能从定义它的函数外部访问它。
For example, the getName()
function returns a variable which is locally called data
but you access the value by calling the function.例如,
getName()
函数返回一个本地称为data
的变量,但您可以通过调用该函数来访问该值。
def getName(user_id):
data = "Your name is " + str(user_id)
return data
# Throws an error, because data in undefined
name = getName("Bobby")
print(data)
# Working code, prints "Your name is Bobby"
name = getName("Bobby")
print(name)
There are no rules against calling one function from inside another.没有禁止从另一个函数内部调用一个函数的规则。 Instead of
a = data.readlines()
try a = getFastaFromFile("dna.fasta')
as well as changing data = inf.read()
to data = inf.readlines()
而不是
a = data.readlines()
尝试a = getFastaFromFile("dna.fasta')
以及将data = inf.read()
更改为data = inf.readlines()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.