2016-04-17 3 views
0

Ich schreibe die Handlung eines Films von OMDB API. Ich möchte den Plot-Wert innerhalb der Ausgabe auf 80 beschränken. Ich habe gesucht, konnte aber nichts finden. Die jsonvalues ​​['Plot'] können mehr als 80 Zeichen enthalten. Ich möchte seinen Wert in mehreren Zeilen ausgeben.Wie wird der Wert einer Variablen in mehreren Zeilen in Python gedruckt?

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" 
+0

Posted eine Antwort. überprüfen Sie, ob es das ist, was Sie wollen. –

Antwort

2

Check-out, wenn dies Sie wollten,

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. 

Es gibt alle Arten von Funktionen in textwrap Modul. Schau sie dir an.

2

TextWrap Modul tut es.

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

Obwohl die Ausgabe auf 80 Zeichen begrenzt wird, wird die Ausgabe abgeschnitten. Ich möchte eine Lösung, um die restlichen 80 * n Zeichen in mehreren Zeilen zu drucken. Daher werden die Daten nicht abgeschnitten. –

+0

Oh, ich sehe ja, dann willst du das textwrap Modul. –

Verwandte Themen