2011-04-26 52 views
1

我正在创建一个函数,该函数从其他函数中读取数据并将其写入到我的桌面上的文本文件中。在书面行之间创建空间

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在文本文件中一个接一个地打印行。我如何打印到下一行并在行之间添加空格?

回答

2

而不是写入,使用writelines,它将序列中的所有内容打印到自己行上的文件中。添加一个空白字符串以添加双倍间距

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

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

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

+0

在我的代码而言,哪里断行去? – Francis 2011-04-26 17:24:16

+0

@Francis在你想要换行的地方,'\ n'就在字符串中。看起来像是在'%s'之后 – Matthew 2011-04-27 21:21:43