【问题标题】:How to print the value of a variable in multiple lines in python? [duplicate]如何在python中多行打印变量的值? [复制]
【发布时间】:2016-04-17 16:05:04
【问题描述】:

我正在使用 OMDB API 编写电影情节。我想将绘图值限制在输出中的 80 长度内。我已经搜索过,但找不到任何东西。 jsonvalues['Plot'] 可以包含超过 80 个字符。我想在多行中打印它的值。

import urllib
import json

name = raw_input('Enter the movie name >')
url = "http://www.omdbapi.com/?t="+name+"&r="+"json"
response = urllib.urlopen(url).read()
jsonvalues = json.loads(response)

if jsonvalues["Response"]=="True":
    print jsonvalues["imdbRating"]
    print 'The plot of the movie is: '+jsonvalues['Plot']
else:
    print "The movie name was not found"

【问题讨论】:

  • 发布了答案。检查它是否是你想要的。

标签: python json imdb


【解决方案1】:

看看这是不是你想要的,

In [408]: import textwrap
In [409]: s = "This is a long line. "*15
In [410]: w = 75 # width
In [411]: print(textwrap.fill(s, w))
This is a long line. This is a long line. This is a long line. This is a
long line. This is a long line. This is a long line. This is a long line.
This is a long line. This is a long line. This is a long line. This is a
long line. This is a long line. This is a long line. This is a long line.
This is a long line.

textwrap 模块中有各种功能。看看他们。

【讨论】:

    【解决方案2】:

    textwrap 模块做到了。

    import textwrap #insert at top of code
    print "\n".join(textwrap.wrap('The plot of the movie is: ' + jsonvalues['Plot'],80))
    

    【讨论】:

    • 虽然它将输出限制为 80 个字符,但它会截断输出。我想要一个解决方案以多行打印其余 80*n 个字符。因此,数据不会被截断。
    • 哦,我明白了,是的,那么您需要 textwrap 模块。
    猜你喜欢
    • 2010-10-10
    • 1970-01-01
    • 2021-06-27
    • 2015-10-28
    • 2012-10-02
    • 2014-06-28
    • 2012-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多