如何在python中多行打印一个变量的值?
How to print the value of a variable in multiple lines in python?
我正在写来自 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"
textwrap 模块做到了。
import textwrap #insert at top of code
print "\n".join(textwrap.wrap('The plot of the movie is: ' + jsonvalues['Plot'],80))
看看这是不是你想要的,
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
模块中有各种功能。检查出来。
我正在写来自 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"
textwrap 模块做到了。
import textwrap #insert at top of code
print "\n".join(textwrap.wrap('The plot of the movie is: ' + jsonvalues['Plot'],80))
看看这是不是你想要的,
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
模块中有各种功能。检查出来。