0

I am writing the plot of a movie from OMDB API. I want to limit the plot value to be within 80 length in the output. I have searched but, could not found anything. The jsonvalues['Plot'] can contain more than 80 characters. I want to print its value in multiple lines.

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"
Soumendra
  • 1,518
  • 3
  • 27
  • 54

2 Answers2

2

textwrap module does it.

import textwrap #insert at top of code
print "\n".join(textwrap.wrap('The plot of the movie is: ' + jsonvalues['Plot'],80))
Miles Gillham
  • 284
  • 1
  • 11
  • Though it's lmiting the output to 80 characters, however it's truncating the output. I want a solution to print the rest 80*n characters in multiple lines. Therefore, the data will not truncate. – Soumendra Apr 17 '16 at 07:44
  • Oh I see, yes, then you want the textwrap module. – Miles Gillham Apr 17 '16 at 08:03
2

Check out if this is you wanted,

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.

There are all kinds of functions in textwrap module. Check them out.

C Panda
  • 3,297
  • 2
  • 11
  • 11