右对齐不工作[Python]
我正在编写代码,以获取用户输入的企业名称并打印出对其的评论。当我运行我的最后一个循环时,我告诉python用四个空格右对齐评论,但什么也没发生。我已经尝试了多种解决方案,但我真的不知所措 (问题区域是最后一行)右对齐不工作[Python],python,loops,padding,justify,Python,Loops,Padding,Justify,我正在编写代码,以获取用户输入的企业名称并打印出对其的评论。当我运行我的最后一个循环时,我告诉python用四个空格右对齐评论,但什么也没发生。我已经尝试了多种解决方案,但我真的不知所措 (问题区域是最后一行) 我建议您验证输出print w.fill(查看列表[I],) 长度可能小于4。所以它看起来不起作用。e、 g.'abcdef'.rjust(4'') “用4个空格右对齐”没有意义,因此不清楚您真正想要什么。.rjust()的第一个参数是字段的总宽度,如果字符串已经至少有那么长,则什么也不
我建议您验证输出
print w.fill(查看列表[I],)
长度可能小于4。所以它看起来不起作用。e、 g.'abcdef'.rjust(4'')
“用4个空格右对齐”没有意义,因此不清楚您真正想要什么。.rjust()
的第一个参数是字段的总宽度,如果字符串已经至少有那么长,则什么也不做。一些例子:
>>> "abcde".rjust(4, " ") # nothing done: 5 > 4
'abcde'
>>> "abcd".rjust(4, " ") # nothing done: 4 == 4
'abcd'
>>> "abc".rjust(4, " ") # extends to 4 with 1 blank on left
' abc'
>>> "ab".rjust(4, " ") # extends to 4 with 2 blanks on left
' ab'
>>> "a".rjust(4, " ") # extends to 4 with 3 blanks on left
' a'
>>> "".rjust(4, " ") # # extends to 4 with 4 blanks
' '
假设您确实想要缩进文本,可以使用
TextWrapper
对象:
indent = ' ' * 4
w = textwrap.TextWrapper(replace_whitespace=False, initial_indent=indent, subsequent_indent=indent)
演示
>>> indent = ' ' * 4
>>> w = textwrap.TextWrapper(width=20, replace_whitespace=False, initial_indent=indent, subsequent_indent=indent)
>>> print(w.fill('A longish paragraph to demonstrate indentation with TextWrapper objects.'))
A longish
paragraph to
demonstrate
indentation with
TextWrapper
objects.
请注意,缩进包含在线宽中,因此您可能需要相应地调整宽度:
>>> w = textwrap.TextWrapper(width=20+len(indent), replace_whitespace=False, initial_indent=indent, subsequent_indent=indent)
>>> print(w.fill('A longish paragraph to demonstrate indentation with TextWrapper objects.'))
A longish paragraph
to demonstrate
indentation with
TextWrapper objects.
很可能它不起作用,因为
wrap()
返回的单个字符串长度远远超过4个字符。例如:
'hello'.rjust(3, '*')
输出:
'hello'
然而,如果您这样做:
“你好。”rjust(10,*)
输出:
'*****hello'
因此,如果我理解您试图执行的操作,您可能需要拆分包装的字符串,然后在打印时对列表中的每个字符串应用正确的对齐方式:
wrapped = w.fill(reviewlist[i], )
for line in wrapped.split('\n'):
print line.rjust(4, ' ')
虽然我不确定仅在四个字符的宽度上进行辩护是否真的是您所需要的。您在这里面临着几个问题:
通过固定空间字体格式化文本非常老派,但也非常脆弱。也许您可以考虑在应用程序的后续版本中生成HTML输出。HTML表格可以很好地实现这一点,并且适用于表格数据。或者,考虑做一个CSV文件,然后你可以把结果导入Excel。< /P>你能提供一个输入、期望输出和实际输出的例子吗?通过“正当化”,你实际上是指“缩进”吗??这可能是正确的答案,但在答案中添加一些澄清,而不是仅仅发布代码片段,这被认为是一种良好的做法。像这样的答案往往会被否决和/或删除。@Markopovic-谢谢你指出这一点。我会记住这一点
import textwrap
import re
# A sample input string.
inputStr = 'This is a long string which I want to right justify by 70 chars with four spaces on left'
# Wrap by 70 (default) chars. This would result in a multi-line string
w = textwrap.fill(inputStr, )
# Using RegEx read the lines and right justify for 75 chars.
m = re.sub("^(\w+.*)$", lambda g : g.group(0).rjust(75), w, flags = re.MULTILINE)
# Print the result
print(m)
wrapped = w.fill(reviewlist[i], )
for line in wrapped.split('\n'):
print line.rjust(4, ' ')
import textwrap
import re
# A sample input string.
inputStr = 'This is a long string which I want to right justify by 70 chars with four spaces on left'
# Wrap by 70 (default) chars. This would result in a multi-line string
w = textwrap.fill(inputStr, )
# Using RegEx read the lines and right justify for 75 chars.
m = re.sub("^(\w+.*)$", lambda g : g.group(0).rjust(75), w, flags = re.MULTILINE)
# Print the result
print(m)