Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 在Python中的每个字符串前后添加一个空格_Python 3.x - Fatal编程技术网

Python 3.x 在Python中的每个字符串前后添加一个空格

Python 3.x 在Python中的每个字符串前后添加一个空格,python-3.x,Python 3.x,我想在Python中的每个字符串前后添加一个空格。我有851个文件。第一个文件包含219行。最后一个文件包含1069行。有些线只是点,而有些线是数字。我想使用center()函数。我试过: import os, os.path for x in range(1, 852): input_file_name = f"9.{x}.txt" output_file_name = os.path.join(f"10.{x}.txt") with open(input_file_

我想在Python中的每个字符串前后添加一个空格。我有851个文件。第一个文件包含219行。最后一个文件包含1069行。有些线只是点,而有些线是数字。我想使用center()函数。我试过:

import os, os.path

for x in range(1, 852):
    input_file_name = f"9.{x}.txt"
    output_file_name = os.path.join(f"10.{x}.txt")
    with open(input_file_name) as input_file:
        with open(output_file_name, "w") as output_file:
            for input_line in input_file:
                output_line = input_line.center(2)
                output_file.write(output_line)
这不会添加任何空间。我希望每个字符串前有一个空格,每个字符串后有一个空格

输入 预期产量
注意:
x
代表空格。任何帮助都将不胜感激。谢谢。

上述代码中的每个
输入\u行
将在末尾包含一个换行符
\n
,因此在您的情况下,您需要删除
\n
字符,因此我们可以使用
rstrip()
根据需要删除换行符并格式化行符

代码 f中输入行的
:
output_line=“”+input_line.rstrip(“\n”)+“\n”
输出文件。写入(输出行)
输出
。
. 
. 
. 
. 
25
. 
. 
. 
. 
. 
. 


上述代码中的每个
输入\u行
将在末尾包含一个换行符
\n
,因此在您的情况下,您需要删除
\n
字符,因此我们可以使用
rstrip()
根据需要删除换行符并格式化行符

代码 f中输入行的
:
output_line=“”+input_line.rstrip(“\n”)+“\n”
输出文件。写入(输出行)
输出
。
. 
. 
. 
. 
25
. 
. 
. 
. 
. 
. 

我的黄色驾驶室

正如我正确理解的那样,您可以使用.join string方法向字符串的开头和结尾添加任何值

#insert character into string with string-method .join

string_1 = 'this is a string without a space at the beginning or end. Put a X where the spacing should be'


iterable_join = ['X', 'X',] 
string_1 = string_1.join(iterable_join)
print(string_1)
我的控制台中的输出是:

X这是一个在开头或结尾没有空格的字符串。放一个x 其中间距应为x

我不知道这是否会造成太多的开销,但它确实有效。 有关更多信息,请参阅:

如果这有助于你解决问题,请标记为答案。 最好的, CJ

莫因黄色驾驶室

正如我正确理解的那样,您可以使用.join string方法向字符串的开头和结尾添加任何值

#insert character into string with string-method .join

string_1 = 'this is a string without a space at the beginning or end. Put a X where the spacing should be'


iterable_join = ['X', 'X',] 
string_1 = string_1.join(iterable_join)
print(string_1)
我的控制台中的输出是:

X这是一个在开头或结尾没有空格的字符串。放一个x 其中间距应为x

我不知道这是否会造成太多的开销,但它确实有效。 有关更多信息,请参阅:

如果这有助于你解决问题,请标记为答案。 最好的,
CJ

每条线的长度很重要!所以要考虑到这一点。每条线的长度很重要!所以要考虑到这一点。这不会在数字后面添加任何空格。好的。。是否强制使用中心法。原因是数字有两个数字,因此取中心将保留最后2个位置的数字是的,我想使用中心方法。不使用中心将很容易
output\u line=“”+input\u line.rstrip(“\n”)+“\n”
output\u line=“”+input\u line.rstrip(“\n”)+“\n”
正在工作。谢谢。这不会在数字后添加任何空格。好的。。是否强制使用中心法。原因是数字有两个数字,因此取中心将保留最后2个位置的数字是的,我想使用中心方法。不使用中心将很容易
output\u line=“”+input\u line.rstrip(“\n”)+“\n”
output\u line=“”+input\u line.rstrip(“\n”)+“\n”
正在工作。非常感谢。
#insert character into string with string-method .join

string_1 = 'this is a string without a space at the beginning or end. Put a X where the spacing should be'


iterable_join = ['X', 'X',] 
string_1 = string_1.join(iterable_join)
print(string_1)