如何使用 Python 在 bash 脚本上显示重音词 JSON
How to display accented words JSON on bash script with Python
我试图在结果中显示带重音的数组,但只显示没有重音的数组。
完成电影 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.",
Shell代码:
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
。
我试图在结果中显示带重音的数组,但只显示没有重音的数组。
完成电影 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.",
Shell代码:
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
。