在python中写入文本文件时如何在单词之间留出空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27826214/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:20:00  来源:igfitidea点击:

How do I make a space between words when writing to a text file in python

pythonwriting

提问by user3662438

The following code writes to a text file

以下代码写入文本文件

if classno== '1':
    f = open("class1.txt", "a")
if classno== '2':
    f = open("class2.txt", "a")
if classno== '3':
    f = open("class3.txt", "a")
f.write(name) 
f.write(score)
f.close()

However, in the text file the name and score do not have space between them for example, how could I change "James14" in to "James 14"

但是,在文本文件中,名称和分数之间没有空格,例如,我如何将“James14”更改为“James 14”

回答by Bhargav Rao

You can try

你可以试试

f.write(name) 
f.write(' ') 
f.write(score)

Or

或者

f.write(name + ' ') 
f.write(score)

Or

或者

f.write(name ) 
f.write(' ' +score)

Or

或者

f.write("{} {}".format(name,score)) 

Or

或者

f.write("%s %s"%(name,score)) 

Or

或者

f.write(" ".join([name,score]))

回答by Martijn Pieters

You'll have to write that space:

你必须写那个空间:

f.write(name) 
f.write(' ') 
f.write(score)

or use string formatting:

或使用字符串格式:

f.write('{} {}'.format(name, score))

If you are using Python 3, or used from __future__ import print_function, you could also use the print()function, and have it add the space for you:

如果您使用的是 Python 3 或 used from __future__ import print_function,您也可以使用print()函数,并让它为您添加空间:

print(name, score, file=f, end='')

I set endto an empty string, because otherwise you'll also get a newline character. Of course, you mayactually wantthat newline character, if you are writing multiple names and scores to the file and each entry needs to be on its own line.

我设置end为一个空字符串,否则你也会得到一个换行符。当然,如果您要在文件中写入多个名称和分数,并且每个条目都需要在自己的行上,则您实际上可能需要该换行符。

回答by Cory Kramer

A simple way would be to simply concatenate with a space character

一种简单的方法是简单地连接一个空格字符

f.write(name + ' ' + score)

A more robust method (for if the formatting gets more involved) is to use the formatmethod

一个更健壮的方法(如果格式变得更复杂)是使用该format方法

f.write('{} {}'.format(name, score))

回答by Mitchell Gouzenko

You should format a string.

你应该格式化一个字符串。

output = "%(Name)s %(Score)s" %{'Name': name, 'Score':score}
f.write(output)
f.close()

Basically, %(Name)s is a placeholder (denoted by the %) for a string (denoted by the s following the parentheses), which we will reference by "Name". Following our format string, which is wrapped in "", we have this weird thing:

基本上,%(Name)s 是一个字符串(由括号后面的 s 表示)的占位符(由 % 表示),我们将通过“Name”来引用它。在我们用 "" 包裹的格式字符串之后,我们有一个奇怪的东西:

%{'Name': name, 'Score':score}

%{'Name': name, 'Score':score}

This is a dictionary that provides replacements for the "Name" and "Score" placeholders.

这是一个提供“名称”和“分数”占位符替换的字典。

回答by Cassandra

Bhargav and Martjin's answers are good. There are many ways to do it. I'd like to add that the .formatway seems to be a little better because you can potentially reuse the arguments and organize your code better.

Bhargav 和 Martjin 的回答很好。有很多方法可以做到。我想补充一点,这种.format方式似乎更好一些,因为您可以重用参数并更好地组织代码。