python-3.x - 输出保持垂直打印
问题描述
我能够编写一个 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”不是字符串。我怎样才能解决这个问题?
解决方案
for lyrin in lyric_info["lyrics"]
将遍历所有chars
Usefor 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')
推荐阅读
- html - 使用 flex 网格系统引导此标头的最佳方法是什么?
- c++ - 使用 GCC 4.8.5 构建的应用程序中对 cxx11 函数的未定义引用
- apache-spark-sql - 为什么 Spark 总是向 HDFS 写入相同数量的文件?
- count - sparql 选择多个计数返回不同的结果
- r - 如何屏蔽给定选定条目的向量
- css - 使用 CSS3 变量不工作来设置背景图像 url?
- android - 微调器不显示 Android
- sql - SQl ANY 或 ALL 子句
- javascript - 即使在其他链接路由上,Angular Routing routerLinkActive 类也不会从基本页面中删除
- javascript - 如何在 CSS 中引用 document.createElement