如何在python中打印多行变量的值?

时间:2016-04-17 07:35:23

标签: python json imdb

我正在编写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"

2 个答案:

答案 0 :(得分:2)

textwrap 模块可以做到。

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

答案 1 :(得分:2)

检查这是否是你想要的,

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模块中有各种功能。检查一下。