如何用Python在bash脚本上显示带重音的单词JSON



我试图在结果中显示带有重音的数组,但只显示没有重音的数组。

完成moviedb API:https://api.themoviedb.org/3/movie/566525?api_key=b2f8880475c888056b6207067fbaa197&language=pt BR

"genres": [
{
"id": 28,
"name": "Ação"
},
{
"id": 12,
"name": "Aventura"
},
{
"id": 14,
"name": "Fantasia"
}
],
"overview": "Shang-Chi precisa confrontar o passado que pensou ter deixado para trás. Ao mesmo tempo, ele é envolvido em uma teia de mistérios da organização conhecida como Dez Anéis.",

外壳代码:

getMovieInfo()
{
movieInfo=$(httpGet "https://api.themoviedb.org/3/movie/566525?api_key=b2f8880475c888056b6207067fbaa197&append_to_response=videos&language=pt-BR")
genreOne=$(echo "$movieInfo" | python -c "import sys, json; print json.load(sys.stdin)['genres'][0]['name']" 2> /dev/null )
genreTwo=$(echo "$movieInfo" | python -c "import sys, json; print json.load(sys.stdin)['genres'][1]['name']" 2> /dev/null )
genreThree=$(echo "$movieInfo" | python -c "import sys, json; print json.load(sys.stdin)['genres'][2]['name']" 2> /dev/null )
genres=$(echo "$genreOne $genreTwo $genreThree" | tr " " ",")
overview=$(echo "$movieInfo" | python -c "import sys, json; print json.load(sys.stdin)['overview']" 2> /dev/null )
}

结果:

==========================================
| Title: Shang-Chi and the Legend of the Ten Rings
| Language: en
| Genre: ,Aventura,Fantasia
| Runtime: 132 mins
| Overview:
==========================================

这里有一种在jq中更干净的方法。这个解决方案还可以更好地扩展(您不需要知道阵列中元素的数量(

my_function() {
movieInfo="$(httpGet "https://api.themoviedb.org/3/movie/566525?api_key=b2f8880475c888056b6207067fbaa197&append_to_response=videos&language=pt-BR")"
language="$(jq -r '.original_language' <<< $movieInfo)"
genres="$(jq -r '[.genres[].name] | join(",")' <<< $movieInfo)"
runtime="$(jq -r '.runtime' <<< $movieInfo)"
overview="$(jq -r '.overview' <<< $movieInfo)"
}

我会在Python中完成所有操作。

为了测试,我还显示了数据中的所有键,这样我就可以看到我还能显示什么了。

import requests
url = "https://api.themoviedb.org/3/movie/566525?api_key=b2f8880475c888056b6207067fbaa197&append_to_response=videos&language=pt-BR"
r = requests.get(url)
data = r.json()
print('keys:', data.keys())
print('---')
print('Title:', data['title'])
print('Language:', data['original_language'])
genres = ",".join(x['name'] for x in data['genres'])
print('Genres:', genres)
print('Runtime:', data['runtime'])
print('Overview:', data['overview'])

结果:

keys: ['adult', 'backdrop_path', 'belongs_to_collection', 'budget', 'genres', 'homepage', 'id', 'imdb_id', 'original_language', 'original_title', 'overview', 'popularity', 'poster_path', 'production_companies', 'production_countries', 'release_date', 'revenue', 'runtime', 'spoken_languages', 'status', 'tagline', 'title', 'video', 'vote_average', 'vote_count', 'videos']
---
Title: Shang-Chi e a Lenda dos Dez Anéis
Language: en
Genres: Ação,Aventura,Fantasia
Runtime: 132
Overview: Shang-Chi precisa confrontar o passado que pensou ter deixado para trás. Ao mesmo tempo, ele é envolvido em uma teia de mistérios da organização conhecida como Dez Anéis.

在Linux Mint 20上测试(基于Ubuntu 20.04(,Python 3.8


但问题可能是如果您使用Python 2(它总是在编码方面有问题(,或者如果您的终端无法显示UTF-8。Python在开始时试图识别终端中使用了什么编码,并使用这种编码来显示结果。有时问题是,当数据被piped发送到其他程序时,因为它没有提供信息,什么编码使用piping,它自动使用ASCII,然后你可能不得不使用sys.stdout.write(),并在写入之前手动编码到UTF-8

最新更新