配列から、関数に複数の引数を送るやり方が分からず困っています。
また、
以下のエラーが発生してしまいます。
ValueError: could not convert string to float: max_wind_direction
以下がプログラム一部抜粋です。
# coding: utf-8
import codecs
import math
class JMACSVReader(object):
def __init__(self, filename,format,encoding='shift_jisx0213', header_ofs=6):
self.format = format
self._istream = codecs.open(filename, 'r', encoding)
for i in range(header_ofs):
self._istream.readline() # ヘッダを読み飛ばす
def _parse_line(self,line):
record = {}
for k,v in zip(self.format, line.split(',')):
if k is not None:
record[k] = v
return record
def __iter__(self):
for line in self._istream:
yield(self._parse_line(line))
weather_codes = [
(u'薄', 'Usu'),(u'大', 'Oo'),
(u'暴風雨', 'Bofu'), (u'快晴', 'Kaisei'),
(u'雨', 'Ame'),(u'晴', 'Hare'),
(u'雪', 'Yuki'), (u'曇', 'Kumori'),
(u'みぞれ', 'Mizore'), (u'ひょう', 'Hyo'),
(u'霧', 'Kiri'),
]
sincos_codes = [
(u'北','0'),(u'東','90'),
(u'南','180'),(u'西','270'),
(u'北北東','23'),(u'北東','45'),
(u'東北東','68'),(u'東南東','113'),
(u'南東','135'),(u'南南東','158'),
(u'南南西','203'),(u'南西','225'),
(u'西南西','248'),(u'西北西','293'),
(u'北西','315'),(u'北北西','338'),
]
wind_direction_codes = [
(u'北', 'N'),(u'東','S'),
(u'南','S'),(u'西','N'),
(u'北北東','N'),(u'北東','N'),
(u'東北東','N'),(u'東南東','S'),
(u'南東','S'),(u'南南東','S'),
(u'南南西','S'),(u'南西','S'),
(u'西南西','E'),(u'西北西','N'),
(u'北西','N'),(u'北北西','N'),
]
def ame(weather):
yuki_length=weather.find(u'雪')
ame_length=weather.find(u'雨')
if(yuki_length==-1):
weather= u'雨'
return weather
if(ame_length<yuki_length):
weather= u'雨'
return weather
else:
weather= u'雪'
return weather
def ame_yuki(weather):
if u'雨' in weather:
weather=ame(weather)
return weather
elif u'雪' in weather:
weather= u'雪'
return weather
elif u'みぞれ' in weather:
weather= u'雪'
return weather
elif u'ひょう' in weather:
weather= u'雪'
return weather
else:
return weather
def wind_direction(wind):
for k,v in wind_direction_codes:
wind = re.sub('^%s$'%k,v,wind.strip())
return wind
def exchange_cos(rad):
test=float(rad)
test=math.radians(test)
hi=math.cos(test)
return hi
def wind_exchange_cos(wind_s,wind):
for k,v in sincos_codes:
wind = re.sub('^%s$'%k,v,wind.strip())
wind=exchange_cos(wind)
#test=math.radians(wind)
#hi=math.sin(test)
wind=wind_s*wind
#print str(wind).encode('utf-8')
return str(wind).encode('utf-8')
def exchange_sin(rad):
test=float(rad)
test=math.radians(test)
hi=math.sin(test)
return hi
def wind_exchange_sin(wind_s,wind):
for k,v in sincos_codes:
wind = re.sub('^%s$'%k,v,wind.strip())
wind=exchange_sin(wind)
#test=math.radians(wind)
#hi=math.sin(test)
wind=wind_s*wind
#print str(wind).encode('utf-8')
return str(wind).encode('utf-8')
def encode_weather(weather):
# 日本語があると何かと面倒なのでコード値にしてしまう。彦根市は上記のルールだけで大丈夫だったが、他の地域や期間に対応するにはパターンを増やす必要があるかも。
# 「晴れ一時雨後曇り」とか後ろについてくるやつは面倒くさいので除去する。
weather=ame_yuki(weather)
wcode = re.sub( u'(一時|時々|後|、).*','',weather.strip())
for k,v in weather_codes:
wcode = wcode.replace(k,v)
return wcode
def format_file_main(filename, in_format, out_format):
jmacsv = JMACSVReader(filename, in_format)
for record in jmacsv:
line = []
for of in out_format:
if isinstance(of,tuple):
line.append(of[1](record[of[0]]))
else:
line.append(record[of])
print(u'\t'.join(line).encode('utf-8'))
if __name__=='__main__':
# usage: jmaformat.py data.csv
import sys
import re
in_format = [
'date',
'temp_max',None,None,
'temp_min',None,None,
'weather',None,None,
'total_rainfall',None,None,None,
'avg_windspeed',None,None,
'avg_atom',None,None,
'avg_humid',None,None,
'avg_temp',None,None,
'hours_of_sunshine',None,None,None,
'highest_snow_depth',None,None,None,
'max_windspeed',None,'max_wind_direction',None,None,
'max_instantaneous_windspeed',None,'max_instantaneous_wind_direction',None,None,None,None,None,
'avg_vapor_pressure',None,None,
'min_atom',None,None,
'avg_humid',None,None,
'min_humid',None,None,None,
'10min_avg_cloudcover',None,None,
'total_snow_depth',None,None,None,
'total_solar_insolation',None,None,
'1hour_max_rainfall',None,None,None,
]
out_format = ['date','temp_max','temp_min','total_rainfall',
'avg_windspeed','avg_atom','avg_humid','avg_temp', 'hours_of_sunshine',
'highest_snow_depth','max_windspeed',
(wind_exchange_sin(max_windspeed,max_wind_direction)),(wind_exchange_cos(max_windspeed,max_wind_direction)),
'max_instantaneous_windspeed',('max_instantaneous_wind_direction',wind_exchange_sin),('max_instantaneous_wind_direction',wind_exchange_cos),
'avg_vapor_pressure','min_atom','avg_humid','min_humid','10min_avg_cloudcover',
'total_snow_depth','total_solar_insolation','1hour_max_rainfall',('weather', encode_weather ),]
for file in sys.argv[1:]:
format_file_main(file,in_format,out_format)
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 過去に投稿した質問と同じ内容の質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
0
ValueError: could not convert string to float: max_wind_direction
これはstring型からfloat型へ変換できないというエラーです。たぶんソースのどこかmax_wind_direction
が使われていると思うのですが、それに文字列が入っているのが原因だと思います。
なので、配列自体は関数に渡せていると思いますよ!
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
0
配列内のmax_windspeedに戻り値って、渡せているのでしょうか?
out_format =['date',(wind_exchange_sin('max_windspeed','max_wind_direction')),]
つまりwind_exchange_sin
の結果がout_format
に代入されているか?ということですか?
ちゃんと実行されれば代入されていると思いますが、エラーが出てる状態では代入されていないと思います。
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 88.13%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる
2017/01/29 00:32
2017/01/29 00:35
以下の文で文字データから数字に変換しているのですが、
エラーになってしまいます。
wind = re.sub('^%s$'%k,v,wind_s.strip())
2017/01/29 00:45
def exchange(rad):
test=float(rad)
test=math.radians(test)
hi=math.sin(test)
return hi
```
ここに`wind`を渡してるときにエラーが出ていると思います。
今引数にしている変数がどのような型になっているのかを、`type(wind)`のように確認して行く方がいいと思います!