3 回答

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超8個(gè)贊
當(dāng)您寫(xiě)入文件'\n'
時(shí),每次您想在寫(xiě)入的文件的新行上添加某些內(nèi)容時(shí),都需要將 a 連接到字符串的末尾
例如:
output.append("The AT content is" + get_dna_stats() + "% of the DNA sequence." + '\n')
為了解決您的第二個(gè)問(wèn)題,我會(huì)將您的代碼更改為如下所示:
temp = "The AT content is" + get_dna_stats() + "% of the DNA sequence." + '\n'output.append(temp)
當(dāng)您附加到列表并調(diào)用函數(shù)時(shí),它將采用函數(shù)的文字文本而不是調(diào)用它。使用臨時(shí)字符串持有者執(zhí)行此操作將在連接字符串之前調(diào)用該函數(shù)。然后您就可以將字符串附加到列表中

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊
你從來(lái)沒(méi)有告訴你的程序要換一條新線。您可以在"\n"每個(gè)字符串中附加或預(yù)先添加特殊字符,也可以通過(guò)以下方式以系統(tǒng)不可知的方式進(jìn)行
import os
在文件的頂部并像這樣編寫(xiě) write_results 函數(shù):
def write_results(output, filename):
try:
with open("output.csv","w") as csvFile:
writer = csv.writer(csvFile)
for i in output:
csvFile.write(i)
os.write(csvFile, os.linesep) # Add this line! It is a system agnostic newline
except IOError:
print("Error writing file")
添加回答
舉報(bào)