2011-04-26 71 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