写入 .txt 文件并使用“\n”换行时出错 python

Error when writing to a .txt file and using "\n" for a new line python

我认为问题是我需要组合 str 和 int,所以我使用 "(str(myvariable)) 并且括号将其丢弃。不过我不确定。这里是代码:

 #Save
if "ave" in animal: #- the s because of the .strip("s") on line 9
    with open(timeStamped('AnimalCount.txt'),'w') as outf:
        outf.write('Pronghorn: ' + (str(pronghorn)) \n 'Deer: ' + (str(deer))) 
    print " "
    print "Saved"
    print " "

问题出在第 4 行。谢谢!

您需要将换行符放在引号内:

'Pronghorn: ' + (str(pronghorn)) + '\n Deer: ' + (str(deer))