質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Unicode

Unicodeはエンコーディングの標準規格です。1つの文字コード体系で多国語の表現を可能にすることを目指して作られています。

Raspberry Pi

Raspberry Piは、ラズベリーパイ財団が開発した、名刺サイズのLinuxコンピュータです。 学校で基本的なコンピュータ科学の教育を促進することを意図しています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

710閲覧

Raspberry piで天気予報を実行したいのですがエラーが出てしまいます。

Ras5

総合スコア11

Unicode

Unicodeはエンコーディングの標準規格です。1つの文字コード体系で多国語の表現を可能にすることを目指して作られています。

Raspberry Pi

Raspberry Piは、ラズベリーパイ財団が開発した、名刺サイズのLinuxコンピュータです。 学校で基本的なコンピュータ科学の教育を促進することを意図しています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

1クリップ

投稿2017/08/21 03:55

###前提・実現したいこと
Raspberry piで天気予報を読み上げるというプログラムを実行したいのですがエラーがでて実行できませんでした。

リンク内容
上記のリンク先にあった通りプログラムを実行しました。
urllib2についてエラーが出たので以下のプログラムに書き換えています。

try: import urllib.request as urllib2 except ImportError: import urllib2

書き換えたうえでプログラムを実行したところ、同じエラーが発生しました。
そこでベストアンサーに選ばれている方のプログラムを入力したところ、別のエラーが発生し困ったため質問させていただきました。

###発生している問題・エラーメッセージ

jsay 8月21日、12時21分48秒 Traceback (most recent call last): File "tenki4", line 65, in <module> main() File "tenki4", line 16, in main say_weather() File "tenki4", line 33, in say_weather obj = json.loads( unicode(r.read()) ) NameError: name 'unicode' is not defined

###該当のソースコード

#!/usr/bin/env python # -*- conding:utf-8 -*- import shlex import subprocess from datetime import datetime try: import urllib.request as urllib2 except ImportError: import urllib2 import json CMD_SAY = 'jsay' def main(): say_datetime() say_weather() return def say_datetime(): d = datetime.now() text = '%s月%s日、%s時%s分%s秒' % (d.month, d.day, d.hour, d.minute, d.second) text = CMD_SAY + ' ' + text print(text) proc = subprocess.Popen(shlex.split(text)) proc.communicate() return def say_weather(): city = '130010'; # Tokyo 他の地域の方は、番号を変えてください。 json_url = 'http://weather.livedoor.com/forecast/webservice/json/v1' #API URL weather_text = u'%sの天気は%sです。' temperature_text = u'%sの予想最高気温、%s度、予想最低気温、%s度です。' try: r = urllib2.urlopen('%s?city=%s' % (json_url, city) ) jsonStr = unicode(r.read()) print(jsonStr) # 確認表示 obj = json.loads( jsonStr ) print(obj) # 確認表示 title = obj['title'] forecasts = obj['forecasts'] # TODAY cast = forecasts[0] temperature = cast['temperature'] today_w_txt = weather_text % (cast['dateLabel'], cast['telop']) # 最高、最低気温を取得。データが存在しない場合もある temp_max = "" temp_min = "" if temperature['max'] is not None: temp_max = temperature['max']['celsius'] if temperature['min'] is not None: temp_max = temperature['min']['celsius'] today_t_txt = temperature_text % (cast['dateLabel'], temp_max, temp_min) # TOMMOROW cast = forecasts[1] temperature = cast['temperature'] tommorow_w_txt = weather_text % (cast['dateLabel'], cast['telop']) # SAY weather_str = title + ' ' + today_w_txt + ' ' + today_t_txt + ' ' + tommorow_w_txt weather_str = weather_str.encode('utf-8') text = '''%s '%s' ''' % (CMD_SAY, weather_str) print(text) proc = subprocess.Popen(shlex.split(text)) proc.communicate() finally: r.close() return ### Execute if __name__ == "__main__": main()

###試したこと
「name 'unicode' is not defined」について様々なサイトで検索してみましたが、回答を得ることができませんでした。初心者であまり詳しくないです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

元はPython2.xのコードですが、実行環境はPython3.xだと思われます。

とりあえず3.x環境で動作させるにはjsonStr = unicode(r.read())jsonStr = r.read().decode('utf-8')に修正してください。

Python2.x3.xは別の言語といってもよいほど違いがあります。
ネット上のコードを参考にする場合は、どちらのバージョンで書かれたコードか確認し、必要に応じてコードを修正する必要があります(けっこう大変です)。
参考:Python 2 から Python 3 への移植

投稿2017/08/21 04:19

編集2017/08/21 04:20
can110

総合スコア38256

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Ras5

2017/09/05 02:32 編集

返信ありがとうございます。 返信が遅れてしまい、申し訳ありません。 ご指摘の通りpython3で実行していました。バージョンによってエラーが出てしまうという事、大変勉強になりました。 プログラムを編集し実行しましたが以下のプログラムが出力され、「jsay」の部分だけ喋り、天気予報の部分は今だに喋ってもらっていません。 どうかお力を貸していただけると幸いです。 jsay 9月5日、10時24分46秒 {"pinpointLocations":[{"link":"http://weather.livedoor.com/area/forecast/1310100","name":"\u5343\u4ee3\u7530\u533a"},{"link":"http://weather.livedoor.com/area/forecast/1310200","name":"\u4e2d\u592e\u533a"},{"link":"http://weather.livedoor.com/area/forecast/1312000","name":"\u7df4\u99ac\u533a"},{"link":"http://weather.livedoor.com/area/forecast/1312100","name":"\u8db3\u7acb\u533a"},{"link":"http://weather.livedoor.com/area/forecast/1312200","name":"\u845b\u98fe\u533a"},{"link":"http://weather.livedoor.com/area/forecast/1312300","name":"\u6c5f\u6238\u5ddd\u533a"},{"link":"http://weather.livedoor.com/area/forecast/1320100","name":"\u516b\u738b\u5b50\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1320200","name":"\u7acb\u5ddd\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1320300","name":"\u6b66\u8535\u91ce\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1320400","name":"\u4e09\u9df9\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1320500","name":"\u9752\u6885\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1320600","name":"\u5e9c\u4e2d\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1320700","name":"\u662d\u5cf6\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1320800","name":"\u8abf\u5e03\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1320900","name":"\u753a\u7530\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1321000","name":"\u5c0f\u91d1\u4e95\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1321100","name":"\u5c0f\u5e73\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1321200","name":"\u65e5\u91ce\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1321300","name":"\u6771\u6751\u5c71\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1321400","name":"\u56fd\u5206\u5bfa\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1321500","name":"\u56fd\u7acb\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1321800","name":"\u798f\u751f\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1321900","name":"\u72db\u6c5f\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1322000","name":"\u6771\u5927\u548c\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1322100","name":"\u6e05\u702c\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1322200","name":"\u6771\u4e45\u7559\u7c73\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1322300","name":"\u6b66\u8535\u6751\u5c71\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1322400","name":"\u591a\u6469\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1322500","name":"\u7a32\u57ce\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1322700","name":"\u7fbd\u6751\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1322800","name":"\u3042\u304d\u308b\u91ce\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1322900","name":"\u897f\u6771\u4eac\u5e02"},{"link":"http://weather.livedoor.com/area/forecast/1330300","name":"\u745e\u7a42\u753a"},{"link":"http://weather.livedoor.com/area/forecast/1330500","name":"\u65e5\u306e\u51fa\u753a"},{"link":"http://weather.livedoor.com/area/forecast/1330700","name":"\u6a9c\u539f\u6751"},{"link":"http://weather.livedoor.com/area/forecast/1330800","name":"\u5965\u591a\u6469\u753a"}],"link":"http://weather.livedoor.com/area/forecast/130010","forecasts":[{"dateLabel":"\u4eca\u65e5","telop":"\u66c7\u6642\u3005\u6674","date":"2017-09-05","temperature":{"min":null,"max":{"celsius":"26","fahrenheit":"78.8"}},"image":{"width":50,"url":"http://weather.livedoor.com/img/icon/9.gif","title":"\u66c7\u6642\u3005\u6674","height":31}},{"dateLabel":"\u660e\u65e5","telop":"\u96e8\u306e\u3061\u66c7","date":"2017-09-06","temperature":{"min":{"celsius":"21","fahrenheit":"69.8"},"max":{"celsius":"24","fahrenheit":"75.2"}},"image":{"width":50,"url":"http://weather.livedoor.com/img/icon/20.gif","title":"\u96e8\u306e\u3061\u66c7","height":31}}],"location":{"city":"\u6771\u4eac","area":"\u95a2\u6771","prefecture":"\u6771\u4eac\u90fd"},"publicTime":"2017-09-05T09:00:00\u002b0900","copyright":{"provider":[{"link":"http://tenki.jp/","name":"\u65e5\u672c\u6c17\u8c61\u5354\u4f1a"}],"link":"http://weather.livedoor.com/","title":"(C) LINE Corporation","image":{"width":118,"link":"http://weather.livedoor.com/","url":"http://weather.livedoor.com/img/cmn/livedoor.gif","title":"livedoor \u5929\u6c17\u60c5\u5831","height":26}},"title":"\u6771\u4eac\u90fd \u6771\u4eac \u306e\u5929\u6c17","description":{"text":" \u65e5\u672c\u306e\u6771\u306b\u306f\u9ad8\u6c17\u5727\u304c\u3042\u3063\u3066\u6771\u306b\u79fb\u52d5\u3057\u3066\u3044\u307e\u3059\u3002\n\n\u3010\u95a2\u6771\u7532\u4fe1\u5730\u65b9\u3011\n \u95a2\u6771\u7532\u4fe1\u5730\u65b9\u306f\u6674\u308c\u3084\u66c7\u308a\u3067\u4f0a\u8c46\u8af8\u5cf6\u3067\u306f\u975e\u5e38\u306b\u6fc0\u3057\u304f\u964d\u3063\u3066\u3044\u308b\u6240\u304c\u3042\n\u308a\u307e\u3059\u3002\n\n 5\u65e5\u306f\u3001\u65e5\u4e2d\u3092\u4e2d\u5fc3\u306b\u6674\u308c\u308b\u6240\u3082\u3042\u308a\u307e\u3059\u304c\u3001\u6771\u3088\u308a\u306e\u6e7f\u3063\u305f\u7a7a\u6c17\u3084\u6c17\u5727\n\u306e\u8c37\u304c\u63a5\u8fd1\u3059\u308b\u305f\u3081\u3001\u66c7\u308a\u3067\u591c\u306b\u306f\u96e8\u306e\u964d\u308b\u6240\u304c\u3042\u308b\u898b\u8fbc\u307f\u3067\u3059\u3002\n\n 6\u65e5\u306f\u3001\u4f4e\u6c17\u5727\u3084\u524d\u7dda\u306e\u5f71\u97ff\u3067\u3001\u66c7\u308a\u3067\u6642\u3005\u96e8\u3068\u306a\u308b\u3067\u3057\u3087\u3046\u3002\n\n \u95a2\u6771\u8fd1\u6d77\u3067\u306f\u30015\u65e5\u306f\u3046\u306d\u308a\u3092\u4f34\u3044\u6ce2\u304c\u3084\u3084\u9ad8\u304f\u30016\u65e5\u306f\u6ce2\u304c\u3084\u3084\u9ad8\u3044\u3067\n\u3057\u3087\u3046\u3002\n\n\u3010\u6771\u4eac\u5730\u65b9\u3011\n 5\u65e5\u306f\u3001\u66c7\u308a\u3067\u6642\u3005\u6674\u308c\u308b\u3067\u3057\u3087\u3046\u3002\n 6\u65e5\u306f\u3001\u96e8\u3067\u5915\u65b9\u304b\u3089\u66c7\u308b\u898b\u8fbc\u307f\u3067\u3059\u3002","publicTime":"2017-09-05T09:38:00\u002b0900"}} {'publicTime': '2017-09-05T09:00:00+0900', 'copyright': {'title': '(C) LINE Corporation', 'provider': [{'name': '日本気象協会', 'link': 'http://tenki.jp/'}], 'image': {'title': 'livedoor 天気情報', 'url': 'http://weather.livedoor.com/img/cmn/livedoor.gif', 'height': 26, 'width': 118, 'link': 'http://weather.livedoor.com/'}, 'link': 'http://weather.livedoor.com/'}, 'link': 'http://weather.livedoor.com/area/forecast/130010', 'title': '東京都 東京 の天気', 'pinpointLocations': [{'name': '千代田区', 'link': 'http://weather.livedoor.com/area/forecast/1310100'}, {'name': '中央区', 'link': 'http://weather.livedoor.com/area/forecast/1310200'}, {'name': '港区', 'link': 'http://weather.livedoor.com/area/forecast/1310300'}, {'name': '新宿区', 'link': 'http://weather.livedoor.com/area/forecast/1310400'}, {'name': '文京区', 'link': 'http://weather.livedoor.com/area/forecast/1310500'}, {'name': '台東区', 'link': 'http://weather.livedoor.com/area/forecast/1310600'}, {'name': '墨田区', 'link': 'http://weather.livedoor.com/area/forecast/1310700'}, {'name': '江東区', 'link': 'http://weather.livedoor.com/area/forecast/1310800'}, {'name': '品川区', 'link': 'http://weather.livedoor.com/area/forecast/1310900'}, {'name': '目黒区', 'link': 'http://weather.livedoor.com/area/forecast/1311000'}, {'name': '大田区', 'link': 'http://weather.livedoor.com/area/forecast/1311100'}, {'name': '世田谷区', 'link': 'http://weather.livedoor.com/area/forecast/1311200'}, {'name': '渋谷区', 'link': 'http://weather.livedoor.com/area/forecast/1311300'}, {'name': '中野区', 'link': 'http://weather.livedoor.com/area/forecast/1330700'}, {'name': '奥多摩町', 'link': 'http://weather.livedoor.com/area/forecast/1330800'}], 'forecasts': [{'image': {'title': '曇時々晴', 'url': 'http://weather.livedoor.com/img/icon/9.gif', 'height': 31, 'width': 50}, 'telop': '曇時々晴', 'dateLabel': '今日', 'temperature': {'max': {'celsius': '26', 'fahrenheit': '78.8'}, 'min': None}, 'date': '2017-09-05'}, {'image': {'title': '雨のち曇', 'url': 'http://weather.livedoor.com/img/icon/20.gif', 'height': 31, 'width': 50}, 'telop': '雨のち曇', 'dateLabel': '明日', 'temperature': {'max': {'celsius': '24', 'fahrenheit': '75.2'}, 'min': {'celsius': '21', 'fahrenheit': '69.8'}}, 'date': '2017-09-06'}], 'location': {'prefecture': '東京都', 'area': '関東', 'city': '東京'}, 'description': {'publicTime': '2017-09-05T09:38:00+0900', 'text': ' 日本の東には高気圧があって東に移動しています。\n\n【関東甲信地方】\n 関東甲信地方は晴れや曇りで伊豆諸島では非常に激しく降っている所があ\nります。\n\n 5日は、日中を中心に晴れる所もありますが、東よりの湿った空気や気圧\nの谷が接近するため、曇りで夜には雨の降る所がある見込みです。\n\n 6日は、低気圧や前線の影響で、曇りで時々雨となるでしょう。\n\n 関東近海では、5日はうねりを伴い波がやや高く、6日は波がやや高いで\nしょう。\n\n【東京地方】\n 5日は、曇りで時々晴れるでしょう。\n 6日は、雨で夕方から曇る見込みです。'}} jsay 'b'\xe6\x9d\xb1\xe4\xba\xac\xe9\x83\xbd \xe6\x9d\xb1\xe4\xba\xac \xe3\x81\xae\xe5\xa4\xa9\xe6\xb0\x97 \xe6\x98\x8e\xe6\x97\xa5\xe3\x81\xae\xe5\xa4\xa9\xe6\xb0\x97\xe3\x81\xaf\xe9\x9b\xa8\xe3\x81\xae\xe3\x81\xa1\xe6\x9b\x87\xe3\x81\xa7\xe3\x81\x99\xe3\x80\x82''
can110

2017/09/05 07:12

weather_str = weather_str.encode('utf-8') は、python3では不要です。
Ras5

2017/09/06 01:07

can110様、回答ありがとうございます。 無事天気予報を喋ってくれました。懇切丁寧に的確なアドバイスを頂き感謝しかありません。 本当にありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問