###前提・実現したいこと
raspberry piでPythonを使って天気予報を読み上げるというプログラムを実行したいのですがエラーがでて実行できませんでした。
僕が質問する前に他の方が同じ内容で苦しんでいらっしゃったのですが全く同じ内容ででした。その方はプログラムを置いてある場所じゃ無い所で実行していた為実行出来なかったとの事です。
###発生している問題・エラーメッセージ
python
1jsay 3月10日、16時48分8秒 2Traceback (most recent call last): 3 File "tenki.py", line 54, in <module> 4 main() 5 File "tenki.py", line 12, in main 6 say_datetime() 7 File "tenki.py", line 20, in say_datetime 8 proc = subprocess.Popen(shlex.split(text)) 9 File "/usr/lib/python2.7/subprocess.py", line 710, in __init__ 10 errread, errwrite) 11 File "/usr/lib/python2.7/subprocess.py", line 1335, in _execute_child 12 raise child_exception 13OSError: [Errno 8] Exec format error
###該当のソースコード
python
1#!/usr/bin/env python 2# -*- coding:utf-8 -*- 3 4import shlex 5import subprocess 6from datetime import datetime 7import urllib2 8import json 9 10CMD_SAY = 'jsay' 11def main(): 12 say_datetime() 13 say_weather() 14 return 15def say_datetime(): 16 d = datetime.now() 17 text = '%s月%s日、%s時%s分%s秒' % (d.month, d.day, d.hour, d.minute, d.second) 18 text = CMD_SAY + ' ' + text 19 print text 20 proc = subprocess.Popen(shlex.split(text)) 21 proc.communicate() 22 return 23def say_weather(): 24 city = '230010'; # Tokyo 他の地域の方は、番号を変えてください。 25 json_url = 'http://weather.livedoor.com/forecast/webservice/json/v1' #API URL 26 weather_text = u'%sの天気は%sです。' 27 temperature_text = u'%sの予想最高気温、%s度、予想最低気温、%s度です。' 28 try: 29 r = urllib2.urlopen('%s?city=%s' % (json_url, city) ) 30 obj = json.loads( unicode(r.read()) ) 31 title = obj['title'] 32 forecasts = obj['forecasts'] 33 # TODAY 34 cast = forecasts[0] 35 temperature = cast['temperature'] 36 today_w_txt = weather_text % (cast['dateLabel'], cast['telop']) 37 today_t_txt = temperature_text % (cast['dateLabel'], temperature['max']['celsius'], temperature['min']['celsius']) 38 # TOMMOROW 39 cast = forecasts[1] 40 temperature = cast['temperature'] 41 tommorow_w_txt = weather_text % (cast['dateLabel'], cast['telop']) 42 # SAY 43 weather_str = title + ' ' + today_w_txt + ' ' + today_t_txt + ' ' + tommorow_w_txt 44 weather_str = weather_str.encode('utf-8') 45 text = '''%s '%s' ''' % (CMD_SAY, weather_str) 46 print text 47 proc = subprocess.Popen(shlex.split(text)) 48 proc.communicate() 49 finally: 50 r.close() 51 return 52### Execute 53if __name__ == "__main__": 54 main()
###試したこと
下記のサイトさんを拝見しましてエラーが出る構文を調べたりしましたが全く分からずこのサイトに希望をかけた次第です
http://raspi.seesaa.net/article/415530289.html
http://www.tapun.net/raspi/raspberry-pi-talk-weather
###補足情報(言語/FW/ツール等のバージョンなど)
python初心者なので何か目的を持ってプログラミング出来ればと思いpythonが標準で入っているRasbperry piで天気予報を喋ってもらうプログラムを作成したくいろいろ調べていたらたどり着いた次第です
使用いているRaspberry piは
Raspberry Pi3 Model Bです
Open Jtalkはインストール済みでコマンドを入力すればその言葉を喋ってくれるところまでは出来ました。
こういう質問サイトで質問するのは初めてなのでまだまだ言葉足らずやそのほか至らぬ点が多々あると思います。藁をもつかむ思いです、何かアドバイス等頂けらたと思います。よろしくお願いいたします。
***追記
can110 さんにご指摘頂きjsayのプログラムを確認した所「#!/bin/sh」が記述されていませんでした。早速記述し実行した所次のようなエラーが出ました。
↓エラー
python
1 2jsay 3月14日、17時0分6秒 3Traceback (most recent call last): 4 File "tenki2.py", line 52, in <module> 5 main() 6 File "tenki2.py", line 11, in main 7 say_weather() 8 File "tenki2.py", line 35, in say_weather 9 today_t_txt = temperature_text % (cast['dateLabel'], temperature['max']['celsius'], temperature['min']['celsius']) 10TypeError: 'NoneType' object has no attribute '__getitem__' 11
です。
更にcan110 さんに助言して頂きプログラムを追加致しました
↓追加
python
1 2#!/usr/bin/env python 3# -*- coding:utf-8 -*- 4 5import shlex 6import subprocess 7from datetime import datetime 8import urllib2 9import json 10 11CMD_SAY = 'jsay' 12def main(): 13 say_datetime() 14 say_weather() 15 return 16def say_datetime(): 17 d = datetime.now() 18 text = '%s月%s日、%s時%s分%s秒' % (d.month, d.day, d.hour, d.minute, d.second) 19 text = CMD_SAY + ' ' + text 20 print text 21 proc = subprocess.Popen(shlex.split(text)) 22 proc.communicate() 23 return 24def say_weather(): 25 city = '130010'; # Tokyo 他の地域の方は、番号を変えてください。 26 json_url = 'http://weather.livedoor.com/forecast/webservice/json/v1' #API URL 27 weather_text = u'%sの天気は%sです。' 28 temperature_text = u'%sの予想最高気温、%s度、予想最低気温、%s度です。' 29 try: 30 jsonStr = unicode(r.read()) 31 print(jsonStr) # 確認表示 32 obj = json.loads( jsonStr ) 33 print(obj) # 確認表示 34 title = obj['title'] 35 forecasts = obj['forecasts'] 36 # TODAY 37 cast = forecasts[0] 38 temperature = cast['temperature'] 39 today_w_txt = weather_text % (cast['dateLabel'], cast['telop']) 40 # 最高、最低気温を取得。データが存在しない場合もある 41 temp_max = "" 42 temp_min = "" 43 if temperature['max'] is not None: 44 temp_max = temperature['max']['celsius'] 45 if temperature['min'] is not None: 46 temp_max = temperature['min']['celsius'] 47 48 today_t_txt = temperature_text % (cast['dateLabel'], temp_max, temp_min) 49 # TOMMOROW 50 cast = forecasts[1] 51 temperature = cast['temperature'] 52 tommorow_w_txt = weather_text % (cast['dateLabel'], cast['telop']) 53 # SAY 54 weather_str = title + ' ' + today_w_txt + ' ' + today_t_txt + ' ' + tommorow_w_txt 55 weather_str = weather_str.encode('utf-8') 56 text = '''%s '%s' ''' % (CMD_SAY, weather_str) 57 print text 58 proc = subprocess.Popen(shlex.split(text,)) 59 proc.communicate() 60 finally: 61 r.close() 62 return 63### Execute 64if __name__ == "__main__": 65 main() 66
と記述
結果
python
1 2jsay 3月14日、17時4分36秒 3Traceback (most recent call last): 4 File "tenki.py", line 64, in <module> 5 main() 6 File "tenki.py", line 13, in main 7 say_weather() 8 File "tenki.py", line 60, in say_weather 9 r.close() 10NameError: global name 'r' is not defined 11
となりました。
回答2件
あなたの回答
tips
プレビュー