简体   繁体   English

写入 .txt 文件并使用 "\\n" 作为新行 python 时出错

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

I think the problem is that I need to combine str and int so I'm using the "(str(myvariable)) and the parentheses are throwing it off. I'm not sure about that, though. Here is the code:我认为问题是我需要结合 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 " "

the problem is on line 4. Thanks!问题出在第 4 行。谢谢!

您需要在引号内添加换行符:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM