输出保持垂直打印

时间:2018-11-15 20:46:54

标签: python-3.x rest api python-3.4

我能够编程一个API调用来从公共API中检索歌词(特别是Dance Gavin Dance的尴尬。问题是,当它打印时,它逐个字母地打印歌词,而不是按照显示的方式垂直打印在API上。这是代码:

import json
import requests

api_url_base = 'https://api.lyrics.ovh/v1/'
headers = {'Content-Type': 'application/json',
           'charset': 'utf-8'}

def get_lyrics_info():

        api_url ='{0}Dance%20Gavin%20Dance/Awkward'.format(api_url_base)
        response = requests.get(api_url, headers=headers)

 if response.status_code == 200:
       return json.loads(response.content.decode('utf-8'))
   else:
       return None

lyric_info = get_lyrics_info()

if lyric_info is not None:
   print("Here is your info: ")
   for lyrin in lyric_info["lyrics"]:
      print(lyrin)

else:
   print('[!] Request Failed')

这是输出的样子(这只是输出的一部分,只是为了向您展示它的样子):

D
o
n
'
t

m
a
k
e

t
h
i
s

a
w
k
w
a
r
d

我尝试使用wrap()函数,fill()函数,但是变量“ lyrin”不是字符串。我该如何解决?

1 个答案:

答案 0 :(得分:0)

for lyrin in lyric_info["lyrics"]将遍历所有chars 使用for lyrin in lyric_info["lyrics"].split('\n'):

或执行sys.stdout.write(lyrin)

import json
import requests

api_url_base = 'https://api.lyrics.ovh/v1/'
headers = {'Content-Type': 'application/json',
           'charset': 'utf-8'}

def get_lyrics_info():
   api_url ='{0}Dance%20Gavin%20Dance/Awkward'.format(api_url_base)
   response = requests.get(api_url, headers=headers)

   if response.status_code == 200:
       return json.loads(response.content.decode('utf-8'))
   else:
      return None

lyric_info = get_lyrics_info()

if lyric_info is not None:
      print("Here is your info: ")
      for lyrin in lyric_info["lyrics"].split('\n'):
         print(lyrin)

else:
       print('[!] Request Failed')

import json
import requests
import sys

api_url_base = 'https://api.lyrics.ovh/v1/'
headers = {'Content-Type': 'application/json',
           'charset': 'utf-8'}

def get_lyrics_info():
   api_url ='{0}Dance%20Gavin%20Dance/Awkward'.format(api_url_base)
   response = requests.get(api_url, headers=headers)

   if response.status_code == 200:
       return json.loads(response.content.decode('utf-8'))
   else:
      return None

lyric_info = get_lyrics_info()

if lyric_info is not None:
      print("Here is your info: ")
      for lyrin in lyric_info["lyrics"]:
         sys.stdout.write(lyrin)

else:
       print('[!] Request Failed')