简体   繁体   English

在 python 中的文件中写入多行

[英]write multiple lines in a file in python

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.这里的目标是文件 object,line1、line2、line3 是用户输入。 I want to use only a single target.write() command to write this script.我只想使用一个 target.write() 命令来编写这个脚本。 I have tried using the following:我尝试使用以下内容:

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. Python 解释器(我使用的是 Microsoft Powershell)说语法无效。 How would I able to do it?我怎么能做到呢?

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])
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))

another way which, at least to me, seems more intuitive:另一种方式,至少对我来说,似乎更直观:

target.write('''line 1
line 2
line 3''')

I notice that this is a study drill from the book "Learn Python The Hard Way".我注意到这是“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.尽管您在 3 年前问过这个问题,但我还是为新用户发帖,说不要直接在 stackoverflow 中提问。 At least read the documentation before asking.至少在询问之前阅读文档。

And as far as the question is concerned, using writelines is 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 所说,你把括号弄乱了,按照他说的去做。

也可以这样做:

target.write(line1 + "\n" + line2 + "\n" + line3 + "\n")

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 版

这也有效:

target.write("{}" "\n" "{}" "\n" "{}" "\n".format(line1, line2, line3))

You could use the join method of Python strings.您可以使用 Python 字符串的join方法。

target.writelines("\n".join([line1, line2, line3]))
target.write("\n")

From https://docs.python.org/3/library/stdtypes.html#str.join :来自https://docs.python.org/3/library/stdtypes.html#str.join

Return a string which is the concatenation of the strings in iterable.返回一个字符串,它是 iterable 中字符串的串联。

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")

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

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