这是我的基本代码,但我不知道是什么的高清我怎么中心从文件
def centre(s, width=70):
lines = open ('poem.txt ', 'r'). readlines()
stripped = []
for line in lines:
stripped.append(line.strip())
这是我的基本代码,但我不知道是什么的高清我怎么中心从文件
def centre(s, width=70):
lines = open ('poem.txt ', 'r'). readlines()
stripped = []
for line in lines:
stripped.append(line.strip())
你可能想看看进入str.format()
功能之后添加对齐文本。如果你读the documentation你会发现,它具有文本居中的能力:
>>> "{0:^40}".format(" Ministry of Silly Walks ")
' Ministry of Silly Walks '
>>> "{0:=^40}".format(" Ministry of Silly Walks ")
'======= Ministry of Silly Walks ========'
对不起,我不了解基本编程。但是,如何使用您的程序打开文件? –
你已经有一种方法可以在你原来的问题中做到这一点:'lines = open('poem.txt','r')。 readlines()'。只需相应地调整它。 – Santa
它说明名称'行'没有定义 –
Python提供了str.center(width[,fillchar])
方法。
for line in lines:
print(line.center(width))
或类似
http://docs.python.org/3/library/stdtypes.html#string-methods
你想要一个更大的字符串中对齐文本中心?在控制台中?到一个文件?到一个字符串? –
我有一个包含文本文件(poem.txt)的文件,我想在运行该程序后进行中心对齐(我是Python新手,因此对不起,因为缺少语言) –
您想重新设置'poem.txt' , 你意思是?此外,它总是有助于展示示例 - 那些超越语言障碍的示例;) –