在python中的文件中写入多行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21019942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
write multiple lines in a file in python
提问by kartikeykant18
I have the following code:
我有以下代码:
line1 = raw_input("line 1: ")
line2 = raw_input("line 2: ")
line3 = raw_input("line 3: ")
print "I'm going to write these to the file."
target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")
Here target is the file object and line1, line2, line3 are the user inputs. I want to use only a single target.write() command to write this script. I have tried using the following:
这里 target 是文件对象,line1、line2、line3 是用户输入。我只想使用一个 target.write() 命令来编写这个脚本。我尝试使用以下方法:
target.write("%s \n %s \n %s \n") % (line1, line2, line3)
But doesn't that put a string inside another string but if I use the following:
但这不是将一个字符串放入另一个字符串中,但如果我使用以下内容:
target.write(%s "\n" %s "\n" %s "\n") % (line1, line2, line3)
The Python interpreter(I'm using Microsoft Powershell) says invalid syntax. How would I able to do it?
Python 解释器(我使用的是 Microsoft Powershell)说语法无效。我怎么能做到呢?
回答by aIKid
You're confusing the braces. Do it like this:
你混淆了大括号。像这样做:
target.write("%s \n %s \n %s \n" % (line1, line2, line3))
Or even better, use writelines:
或者甚至更好,使用writelines:
target.writelines([line1, line2, line3])
回答by Burhan Khalid
with open('target.txt','w') as out:
line1 = raw_input("line 1: ")
line2 = raw_input("line 2: ")
line3 = raw_input("line 3: ")
print("I'm going to write these to the file.")
out.write('{}\n{}\n{}\n'.format(line1,line2,line3))
回答by Marco_hm
Assuming you don't want a space at each new line use:
假设您不希望在每个新行使用空格:
print("I'm going to write these to the file")
target.write("%s\n%s\n%s\n" % (line1, line2, line3))
This works for version 3.6
这适用于 3.6 版
回答by Sreevathsa Gowru
I notice that this is a study drill from the book "Learn Python The Hard Way". Though you've asked this question 3 years ago, I'm posting this for new users to say that don't ask in stackoverflow directly. At least read the documentation before asking.
我注意到这是“Learn Python The Hard Way”一书中的学习练习。尽管您在 3 年前问过这个问题,但我还是为新用户发帖,说不要直接在 stackoverflow 中提问。至少在询问之前阅读文档。
And as far as the question is concerned, using writelinesis the easiest way.
就问题而言,使用writelines是最简单的方法。
Use it like this:
像这样使用它:
target.writelines([line1, line2, line3])
And as alkid said, you messed with the brackets, just follow what he said.
正如 alkid 所说,你把括号弄乱了,按照他说的去做。
回答by Samrat Bhattacharjee
It can be done like this as well:
也可以这样做:
target.write(line1 + "\n" + line2 + "\n" + line3 + "\n")
回答by jaydeemourg
this also works:
这也有效:
target.write("{}" "\n" "{}" "\n" "{}" "\n".format(line1, line2, line3))
回答by jcklopp
another way which, at least to me, seems more intuitive:
另一种方式,至少对我来说,似乎更直观:
target.write('''line 1
line 2
line 3''')
回答by jainam shah
variable=10
f=open("fileName.txt","w+") # file name and mode
for x in range(0,10):
f.writelines('your text')
f.writelines('if you want to add variable data'+str(variable))
# to add data you only add String data so you want to type cast variable
f.writelines("\n")

