3 回答

TA贡献1824条经验 获得超8个赞
当您写入文件'\n'
时,每次您想在写入的文件的新行上添加某些内容时,都需要将 a 连接到字符串的末尾
例如:
output.append("The AT content is" + get_dna_stats() + "% of the DNA sequence." + '\n')
为了解决您的第二个问题,我会将您的代码更改为如下所示:
temp = "The AT content is" + get_dna_stats() + "% of the DNA sequence." + '\n'output.append(temp)
当您附加到列表并调用函数时,它将采用函数的文字文本而不是调用它。使用临时字符串持有者执行此操作将在连接字符串之前调用该函数。然后您就可以将字符串附加到列表中

TA贡献1828条经验 获得超3个赞
你从来没有告诉你的程序要换一条新线。您可以在"\n"每个字符串中附加或预先添加特殊字符,也可以通过以下方式以系统不可知的方式进行
import os
在文件的顶部并像这样编写 write_results 函数:
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")
添加回答
举报