【问题标题】:Creating space in between written lines在书写的行之间创造空间
【发布时间】:2011-04-26 16:57:20
【问题描述】:

我目前正在创建一个函数,该函数从其他函数读取数据并将其写入我桌面上的文本文件。

def outputResults(filename):
"""This function serves to output and write the results from analyzeGenome.py to a text file \
   Input: filename of output file, dictionary of codon frequencies, dictionary of codon counts \
        GC-content, FASTA header, & sequence length

   Output: Text file containing all the above """

outString = "Header = %s" %header
filename.write(outString)

outString2 = "Sequence Length = %.3F MB" % length
filename.write(outString2)

当我这样做时,python 会在文本文件中逐行打印。如何打印到下一行并在行之间添加空格?

【问题讨论】:

    标签: python file-io io


    【解决方案1】:

    您需要附加一个换行符。 http://docs.python.org/library/stringio.html

    output.write('First line.\n')

    【讨论】:

    • @Francis \n 在字符串中你想要换行的地方。在我看来,它紧随%s
    【解决方案2】:

    使用writelines 代替写入,它将序列中的所有内容打印到单独的文件中。添加空白字符串以添加双倍间距

      filename.writelines([outString, "", outString2]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多