写入.txt文件并使用" \ n"时出错换一个新的线路python

时间:2017-01-13 19:21:16

标签: python python-2.7 newline

我认为问题是我需要结合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行。谢谢!

1 个答案:

答案 0 :(得分:1)

您需要新行字符在引号内:

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