如何将返回值(来自上一个函数)读入pandas、python中?
在下面的节目中 我想从下游函数中的一个函数访问/通过管道传输数据 python代码如下所示:如何将返回值(来自上一个函数)读入pandas、python中?,python,function,pandas,stdin,main,Python,Function,Pandas,Stdin,Main,在下面的节目中 我想从下游函数中的一个函数访问/通过管道传输数据 python代码如下所示: def main(): data1, data2, data3 = read_file() do_calc(data1, data2, data3) def read_file(): data1 = "" data2 = "" data3 = "" file1 = open('file1.txt', 'r+').read() for line in fi
def main():
data1, data2, data3 = read_file()
do_calc(data1, data2, data3)
def read_file():
data1 = ""
data2 = ""
data3 = ""
file1 = open('file1.txt', 'r+').read()
for line in file1
do something....
data1 += calculated_values
file2 = open('file2.txt', 'r+').read()
for line in file1
do something...
data2 += calculated_values
file1 = open('file1.txt', 'r+').read()
for line in file1
do something...
data3 += calculated_values
return data1, data2, data3
def do_calc(data1, data2, data3):
d1_frame = pd.read_table(data1, sep='\t')
d2_frame = pd.read_table(data2, sep='\t')
d3_frame = pd.read_table(data3, sep='\t')
all_data = [d1_frame, d2_frame, d3_frame]
main()
给定的代码有什么问题?看起来panda无法正确读取输入文件,但正在将数据1、2和3中的值打印到屏幕上
read_hdf似乎可以读取文件,但无法正确读取。是否有一种方法可以将函数返回的数据直接读取到pandas中(无需写入/读取文件)
错误消息:
Traceback (most recent call last):
File "calc.py", line 757, in <module>
main()
File "calc.py", line 137, in main
merge_tables(pop1_freq_table, pop2_freq_table, f1_freq_table)
File "calc.py", line 373, in merge_tables
df1 = pd.read_table(pop1_freq_table, sep='\t')
File "/home/everestial007/.local/lib/python3.5/site-packages/pandas/io/parsers.py", line 645, in parser_f
return _read(filepath_or_buffer, kwds)
File "/home/everestial007/.local/lib/python3.5/site-packages/pandas/io/parsers.py", line 388, in _read
parser = TextFileReader(filepath_or_buffer, **kwds)
File "/home/everestial007/.local/lib/python3.5/site-packages/pandas/io/parsers.py", line 729, in __init__
self._make_engine(self.engine)
File "/home/everestial007/.local/lib/python3.5/site-packages/pandas/io/parsers.py", line 922, in _make_engine
self._engine = CParserWrapper(self.f, **self.options)
File "/home/everestial007/.local/lib/python3.5/site-packages/pandas/io/parsers.py", line 1389, in __init__
self._reader = _parser.TextReader(src, **kwds)
File "pandas/parser.pyx", line 373, in pandas.parser.TextReader.__cinit__ (pandas/parser.c:4019)
File "pandas/parser.pyx", line 665, in pandas.parser.TextReader._setup_parser_source (pandas/parser.c:7967)
FileNotFoundError: File b'0.667,0.333\n2\t15800126\tT\tT,A\t0.667,0.333\n2\t15800193\tC\tC,T\t0.667,0.333\n2\t15800244\tT\tT,C\......
回溯(最近一次呼叫最后一次):
文件“calc.py”,第757行,在
main()
文件“calc.py”,第137行,主
合并表格(pop1表格、pop2表格、f1表格)
文件“calc.py”,第373行,在合并表格中
df1=pd.read\u表(pop1\u freq\u表,sep='\t')
文件“/home/everestial007/.local/lib/python3.5/site packages/pandas/io/parsers.py”,第645行,在解析器中
返回读取(文件路径或缓冲区,kwds)
文件“/home/everestial007/.local/lib/python3.5/site packages/pandas/io/parsers.py”,第388行,已读
parser=TextFileReader(文件路径或缓冲区,**kwds)
文件“/home/everestial007/.local/lib/python3.5/site packages/pandas/io/parsers.py”,第729行,在__
自制发动机(自制发动机)
文件“/home/everestial007/.local/lib/python3.5/site-packages/pandas/io/parsers.py”,第922行,在引擎中
self.\u engine=CParserWrapper(self.f,**self.options)
文件“/home/everestial007/.local/lib/python3.5/site packages/pandas/io/parsers.py”,第1389行,在__
self.\u reader=\u parser.textleader(src,**kwds)
文件“pandas/parser.pyx”,第373行,位于pandas.parser.TextReader.\u u cinit__;(pandas/parser.c:4019)中
文件“pandas/parser.pyx”,第665行,位于pandas.parser.TextReader.\u设置\u解析器\u源(pandas/parser.c:7967)
FileNotFoundError:文件b'0.667,0.333\n2\t15800126\tT\tT,A\t0.667,0.333\n2\t15800193\tC\tC,T\t0.667,0.333\n2\t15800244\tT\tT,C\。。。。。。
希望您能给我解释。pd.read\u table(data1,sep='\t')
将data1
视为一个文件路径,因为它没有read
方法。您可以在stacktrace中看到,它试图打开一个名为csv文件内容的文件
从read_表
帮助:
您应该将其转换为io.StringIO
对象,以便读取
快速修复:
pd.read_table(io.StringIO(data1), sep='\t')
但这会创建数据的副本。最好的解决方法是直接创建io.StringIO
缓冲区:
def read_file():
data1 = io.StringIO()
file1 = open('file1.txt', 'r+').read()
for line in file1
do something....
data1.write(calculated_values)
# in the end
data1.seek(0) # reset to start of "file"
pd.read\u表(data1,sep='\t')
将data1
视为文件路径,因为它没有read
方法。您可以在stacktrace中看到,它试图打开一个名为csv文件内容的文件
从read_表
帮助:
您应该将其转换为io.StringIO
对象,以便读取
快速修复:
pd.read_table(io.StringIO(data1), sep='\t')
但这会创建数据的副本。最好的解决方法是直接创建io.StringIO
缓冲区:
def read_file():
data1 = io.StringIO()
file1 = open('file1.txt', 'r+').read()
for line in file1
do something....
data1.write(calculated_values)
# in the end
data1.seek(0) # reset to start of "file"
(1) 我建议您使用pd.read\u csv(file1,sep=“\t”)
而不是read table。(2) 如果每个文件都包含一列,为什么不将它们直接读入熊猫并进行计算?这将使您的生活比编写单独的函数来读取容易得多。(1)我建议您使用pd.read\u csv(file1,sep=“\t”)
而不是read table。(2) 如果每个文件都包含一列,为什么不将它们直接读入熊猫并进行计算?这将使你的生活比编写一个单独的函数来阅读容易得多。