【发布时间】:2014-09-01 11:56:52
【问题描述】:
我正在使用python包https://pypi.python.org/pypi/mygene 做一些基因id映射。我有以下代码:
#! /usr/bin/env python
# ID mapping using mygene
# https://pypi.python.org/pypi/mygene
# http://nbviewer.ipython.org/gist/newgene/6771106
# http://mygene-py.readthedocs.org/en/latest/
# 08/30/14
__author__ = 'tommy'
import mygene
import fileinput
import sys
mg = mygene.MyGeneInfo()
# mapping gene symbols to Entrez gene ids and Ensemble gene ids.
# fileinput will loop through all the lines in the input specified as file names given in command-line arguments,
# or the standard input if no arguments are provided.
# build a list from an input file with one gene name in each line
def get_gene_symbols():
gene_symbols = []
for line in fileinput.input():
gene_symbol = line.strip() # assume each line contains only one gene symbol
gene_symbols.append(gene_symbol)
fileinput.close()
return gene_symbols
Entrez_ids = mg.querymany(get_gene_symbols(), scopes='symbol', fields='entrezgene', species='human', as_dataframe=True)
# set as_dataframe to True will return a pandas dataframe object
Entrez_ids.to_csv(sys.stdout, sep="\t") # write the dataframe to stdout
# sys.stdout.write() expects the character buffer object
Entrez_ids.to_csv("Entrez_ids.txt", sep="\t") # write the pandas dataframe to csv
我的问题是,如果我使用 Entrez_ids.to_csv("Entrez_ids.txt", sep="\t"),结果文件将不会包含 stderr 像 'finished...),但 Entrez_ids.to_csv(sys .stdout, sep="\t") 将打印出 stderr 消息和数据帧。
如果我使用 Entrez_ids.to_csv(sys.stdout, sep="\t") 如何重定向标准错误? 谢谢!
【问题讨论】:
-
在命令行中,您可以使用
python your-file.py 2>error.log.txt来重定向输出。你还想要别的吗? -
谢谢,我知道这个技巧。我做了什么: python my-file.py input.txt 2> /dev/null 但标准错误仍然打印在屏幕上。输入文件是这样的:CCDC83 MAST3 FLOT1 RPL11 ZDHHC20 LUC7L3 SNORD49A CTSH ACOT8,每行一个基因名称。你可以自己测试一下。我认为这是因为 mg.querymany 函数将返回错误消息和数据帧。