如何像tsv一样保存python的输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29895602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 05:08:39  来源:igfitidea点击:

How to save output from python like tsv

pythonsaveoutput

提问by Vonton

I am using biopython package and I would like to save result like tsv file. This output from print to tsv.

我正在使用 biopython 包,我想将结果保存为 tsv 文件。此输出从打印到 tsv。

for record in SeqIO.parse("/home/fil/Desktop/420_2_03_074.fastq", "fastq"):
    print ("%s %s %s" % (record.id,record.seq, record.format("qual")))

Thank you.

谢谢你。

采纳答案by ZdaR

That is fairly simple , instead of printing it you need to write that to a file.

这相当简单,您需要将其写入文件,而不是打印它。

with open("records.tsv", "w") as record_file:
    for record in SeqIO.parse("/home/fil/Desktop/420_2_03_074.fastq", "fastq"):
        record_file.write("%s %s %s\n" % (record.id,record.seq, record.format("qual")))

And if you want to name the various columns in the file then you can use:

如果要命名文件中的各个列,则可以使用:

record_file.write("Record_Id    Record_Seq    Record_Qal\n")

So the complete code may look like:

所以完整的代码可能如下所示:

with open("records.tsv", "w") as record_file:
    record_file.write("Record_Id    Record_Seq    Record_Qal\n")
    for record in SeqIO.parse("/home/fil/Desktop/420_2_03_074.fastq", "fastq"):
        record_file.write(str(record.id)+"  "+str(record.seq)+"  "+ str(record.format("qual"))+"\n")

回答by EvenLisle

The following snippet:

以下片段:

from __future__ import print_function
with open("output.tsv", "w") as f:
  print ("%s\t%s\t%s" % ("asd", "sdf", "dfg"), file=f)
  print ("%s\t%s\t%s" % ("sdf", "dfg", "fgh"), file=f)

Yields a file output.tsvcontaining

产生一个output.tsv包含

asd    sdf    dfg
sdf    dfg    fgh

So, in your case:

所以,在你的情况下:

from __future__ import print_function
with open("output.tsv", "w") as f:
  for record in SeqIO.parse("/home/fil/Desktop/420_2_03_074.fastq", "fastq"):
    print ("%s %s %s" % (record.id,record.seq, record.format("qual")), file=f)

回答by philshem

I prefer using join()in this type of code:

我更喜欢join()在这种类型的代码中使用:

for record in SeqIO.parse("/home/fil/Desktop/420_2_03_074.fastq", "fastq"):
    print ( '\t'.join((str(record.id), str(record.seq), str(record.format("qual"))) )

The 'tab' character is \tand the join function takes the (3) arguments and prints them with a tab in between.

'tab' 字符是\t,join 函数接受 (3) 个参数并在它们之间打印一个制表符。

回答by Doug R.

My preferred solution is to use the CSVmodule. It's a standard module, so:

我的首选解决方案是使用CSV模块。这是一个标准模块,所以:

  • Somebody else has already done all the heavy lifting.
  • It allows you to leverage all the functionality of the CSVmodule.
  • You can be fairly confident it will function as expected (not always the case when I write it myself).
  • You're not going to have to reinvent the wheel, either when you write the file or when you read it back in on the other end (I don't know your record format, but if one of your records contains a TAB, CSVwill escape it correctly for you).
  • It will be easier to support when the next person has to go in to update the code 5 years after you've left the company.
  • 其他人已经完成了所有繁重的工作。
  • 它允许您利用CSV模块的所有功能。
  • 您可以相当有信心它会按预期运行(当我自己编写时,情况并非总是如此)。
  • 无论是在编写文件时还是在另一端读回文件时,您都不必重新发明轮子(我不知道您的记录格式,但如果您的一个记录包含TABCSV将为您正确逃脱)。
  • 当您离开公司 5 年后下一个人必须进来更新代码时,支持会更容易。

The following code snippet should do the trick for you:

以下代码片段应该可以为您解决问题:

#! /bin/env python3
import csv
with open('records.tsv', 'w') as tsvfile:
    writer = csv.writer(tsvfile, delimiter='\t', newline='\n')
    for record in SeqIO.parse("/home/fil/Desktop/420_2_03_074.fastq", "fastq"):
        writer.writerow([record.id, record.seq, record.format("qual")])

Note that this is for Python 3.x. If you're using 2.x, the openand writer = ...will be slightly different.

请注意,这是针对 Python 3.x 的。如果您使用的是 2.x,则openwriter = ...将略有不同。

回答by Domi W

If you want to use the .tsvto label your word embeddings in TensorBoard, use the following snippet. It uses the CSVmodule (see Doug's answer).

如果要使用.tsv来标记 TensorBoard 中的词嵌入,请使用以下代码段。它使用CSV模块(参见Doug 的回答)。

# /bin/env python3
import csv

def save_vocabulary():
    label_file = "word2context/labels.tsv"
    with open(label_file, 'w', encoding='utf8', newline='') as tsv_file:
        tsv_writer = csv.writer(tsv_file, delimiter='\t', lineterminator='\n')
        tsv_writer.writerow(["Word", "Count"])
        for word, count in word_count:
            tsv_writer.writerow([word, count])

word_countis a list of tuples like this:

word_count是一个像这样的元组列表:

[('the', 222594), ('to', 61479), ('in', 52540), ('of', 48064) ... ]