正确地证明没有工作[Python]

时间:2016-05-05 03:00:40

标签: python loops padding justify

我正在处理代码以获取用户输入的商家名称并打印出评论。当我运行我的最后一个循环时,我告诉python通过四个空格右对齐评论,但没有任何反应。我尝试了多种解决方案,老实说不知所措。

(问题区域是最后一行)

import json
import textwrap
import sys
f = open('reviews.json')
f1= open('businesses.json')
line1= f1.readline()
business= json.loads(line1)
line = f.readline()
review = json.loads(line)
idlist=[]
reviewlist=[]
bizname= raw_input('Enter a business name => ')
print bizname

for line in f1:
    business= json.loads(line)
    if bizname == business['name']:
        idlist.append(business['business_id'])

if len(idlist)==0:
    print 'This business is not found'
    sys.exit()

for line in f:
    review = json.loads(line)
    for item in idlist:
        if item == review['business_id']:
            reviewlist.append(review['text'])
if len(reviewlist)==0:
    print 'No reviews for this business are found'
    sys.exit()
for i in range(len(reviewlist)):
    w = textwrap.TextWrapper(replace_whitespace=False)
    print 'Review',str(i+1)+':'
    print w.fill(reviewlist[i] , ).rjust(4,' ') 

6 个答案:

答案 0 :(得分:1)

“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
'    '

答案 1 :(得分:1)

假设您确实希望缩进文本,可以使用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.

答案 2 :(得分:0)

我建议您验证输出print w.fill(reviewlist[i] , )

长度可能小于4.所以它看起来不起作用。例如'abcdef'.rjust(4, ' ')

>>> 'abcdef'.rjust(4, ' ')
'abcdef'
>>> 'abcdef'.rjust(20, ' ')
'              abcdef'

https://docs.python.org/2/library/string.html#string.rjust

答案 3 :(得分:0)

很可能它不起作用,因为wrap()返回一个长于4个字符的字符串。例如:

'hello'.rjust(3, '*')

输出:

'hello'

然而,如果你这样做:     'hello'.rjust(10,'*')

输出:

'*****hello'

所以,如果我理解你要做什么,你可能需要拆分包裹的字符串,然后在打印时将右对齐应用于列表中的每个字符串:

wrapped = w.fill(reviewlist[i], )
for line in wrapped.split('\n'):
    print line.rjust(4, ' ')

虽然我不确定只有四个字符的宽度才能证明你需要它。

答案 4 :(得分:0)

你在这里遇到了几个问题:

  1. .rjust(4,'')表示您希望结果宽度为4个字符,而不是要将该行缩进4个空格。
  2. .rjust()只是查看字符串的长度,并且在通过textwrap运行之后,它有一堆新行,使字符串的长度不同于它打印到的宽度。 / LI>
  3. 你不想正确地证明,实际上,你想要缩进。
  4. 上面给出的关于缩进的解决方案是正确的。

    通过固定空间字体格式化文本是非常古老的学校,但也非常脆弱。也许您可以考虑在应用程序的后续版本中生成HTML输出。 HTML表适用于此,适用于表格数据。或者,考虑执行CSV文件,然后将结果导入Excel。

答案 5 :(得分:0)

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)