2019年1月20日日曜日

開発環境

ということで、アスタリスクを取り除く簡単なコードを書いてみた。(あと文字化けしないようにもしてみた。)

コード

Python 3

#!/usr/bin/env python3
from urllib.request import urlopen

url = 'http://www.data.jma.go.jp/cpdinfo/temp/list/csv/an_wld.csv'
filename = url.split('/')[-1]
with urlopen(url) as req, open(filename, 'w') as f:
    for line in req:
        print(line.decode('shift_jis').replace('*', ''), file=f, end='')

入出力結果(Terminal、cmd(コマンドプロンプト)、Jupyter(IPython))

$ python3 sample.py
$ cat an_wld.csv 
年,世界全体,北半球,南半球
1891,-0.63,-0.68,-0.59
1892,-0.71,-0.80,-0.62
1893,-0.75,-0.87,-0.63
1894,-0.70,-0.73,-0.68
1895,-0.68,-0.75,-0.60
1896,-0.47,-0.53,-0.42
1897,-0.49,-0.53,-0.45
1898,-0.66,-0.65,-0.68
1899,-0.56,-0.58,-0.55
1900,-0.49,-0.48,-0.51
1901,-0.58,-0.55,-0.63
1902,-0.70,-0.75,-0.66
1903,-0.77,-0.78,-0.77
1904,-0.84,-0.88,-0.79
1905,-0.70,-0.72,-0.68
1906,-0.60,-0.59,-0.62
1907,-0.78,-0.85,-0.70
1908,-0.82,-0.84,-0.82
1909,-0.82,-0.84,-0.81
1910,-0.79,-0.78,-0.80
1911,-0.82,-0.79,-0.85
1912,-0.73,-0.83,-0.61
1913,-0.70,-0.79,-0.60
1914,-0.53,-0.57,-0.49
1915,-0.43,-0.47,-0.40
1916,-0.64,-0.70,-0.59
1917,-0.71,-0.77,-0.64
1918,-0.55,-0.61,-0.48
1919,-0.58,-0.68,-0.45
1920,-0.51,-0.56,-0.46
1921,-0.43,-0.39,-0.52
1922,-0.56,-0.56,-0.58
1923,-0.54,-0.54,-0.55
1924,-0.55,-0.53,-0.60
1925,-0.46,-0.42,-0.52
1926,-0.36,-0.33,-0.42
1927,-0.47,-0.43,-0.54
1928,-0.47,-0.47,-0.50
1929,-0.60,-0.62,-0.59
1930,-0.38,-0.33,-0.46
1931,-0.34,-0.31,-0.41
1932,-0.38,-0.38,-0.39
1933,-0.53,-0.57,-0.50
1934,-0.37,-0.37,-0.39
1935,-0.46,-0.45,-0.49
1936,-0.48,-0.43,-0.56
1937,-0.37,-0.32,-0.46
1938,-0.33,-0.26,-0.45
1939,-0.37,-0.34,-0.46
1940,-0.32,-0.39,-0.25
1941,-0.26,-0.31,-0.21
1942,-0.26,-0.31,-0.24
1943,-0.23,-0.21,-0.31
1944,-0.11,-0.15,-0.11
1945,-0.25,-0.33,-0.16
1946,-0.40,-0.36,-0.52
1947,-0.43,-0.43,-0.46
1948,-0.40,-0.36,-0.50
1949,-0.42,-0.40,-0.47
1950,-0.49,-0.52,-0.47
1951,-0.35,-0.31,-0.44
1952,-0.29,-0.31,-0.30
1953,-0.22,-0.18,-0.31
1954,-0.45,-0.43,-0.52
1955,-0.47,-0.42,-0.57
1956,-0.56,-0.61,-0.53
1957,-0.28,-0.32,-0.25
1958,-0.22,-0.19,-0.28
1959,-0.29,-0.30,-0.30
1960,-0.33,-0.32,-0.36
1961,-0.23,-0.25,-0.24
1962,-0.21,-0.19,-0.26
1963,-0.18,-0.16,-0.23
1964,-0.49,-0.50,-0.49
1965,-0.42,-0.48,-0.37
1966,-0.35,-0.33,-0.39
1967,-0.35,-0.32,-0.42
1968,-0.37,-0.37,-0.39
1969,-0.27,-0.38,-0.14
1970,-0.29,-0.36,-0.22
1971,-0.41,-0.48,-0.33
1972,-0.29,-0.49,-0.06
1973,-0.16,-0.26,-0.05
1974,-0.44,-0.54,-0.34
1975,-0.39,-0.42,-0.35
1976,-0.48,-0.58,-0.36
1977,-0.19,-0.25,-0.12
1978,-0.28,-0.34,-0.21
1979,-0.16,-0.25,-0.07
1980,-0.13,-0.22,-0.05
1981,-0.09,-0.05,-0.13
1982,-0.21,-0.29,-0.13
1983,-0.06,-0.13,+0.02
1984,-0.24,-0.36,-0.13
1985,-0.26,-0.40,-0.11
1986,-0.17,-0.25,-0.09
1987,-0.01,-0.10,+0.08
1988,-0.03,-0.06,-0.01
1989,-0.10,-0.12,-0.09
1990,+0.04,+0.08,0.00
1991,-0.02,-0.06,0.00
1992,-0.17,-0.25,-0.09
1993,-0.15,-0.22,-0.06
1994,-0.07,-0.07,-0.08
1995,+0.01,+0.06,-0.04
1996,-0.09,-0.15,-0.04
1997,+0.09,+0.10,+0.07
1998,+0.22,+0.24,+0.19
1999,0.00,+0.03,-0.05
2000,0.00,+0.02,-0.04
2001,+0.12,+0.14,+0.10
2002,+0.16,+0.18,+0.13
2003,+0.16,+0.20,+0.10
2004,+0.12,+0.20,+0.04
2005,+0.17,+0.26,+0.08
2006,+0.16,+0.24,+0.07
2007,+0.12,+0.24,0.00
2008,+0.05,+0.12,-0.02
2009,+0.16,+0.19,+0.15
2010,+0.20,+0.28,+0.12
2011,+0.08,+0.13,+0.02
2012,+0.15,+0.21,+0.09
2013,+0.20,+0.26,+0.15
2014,+0.27,+0.38,+0.17
2015,+0.42,+0.59,+0.27
2016,+0.45,+0.59,+0.31
2017,+0.38,+0.51,+0.25
2018,+0.30,+0.41,+0.20
$

一行だけだから手作業で置換すれば問題ないけど、今後、新しいデータにもしアスタリスクが含まれる行が追加されて役立つ時がきたら嬉しいかも。と言いつつ、そのような時が来なくて含まれない方がもっと嬉しいかも。

0 コメント:

コメントを投稿