回答編集履歴
4
データ異常について追記
test
CHANGED
@@ -1,6 +1,12 @@
|
|
1
1
|
「札幌」を入力した場合はこれで動きました。
|
2
2
|
|
3
3
|
最低・最高気温データは None だったので、それを判断する if文が必要でした。
|
4
|
+
|
5
|
+
simei.txt にコロンのない行があります。
|
6
|
+
|
7
|
+
むつ020020
|
8
|
+
|
9
|
+
八戸020030
|
4
10
|
|
5
11
|
|
6
12
|
|
3
プログラム変更
test
CHANGED
@@ -10,6 +10,38 @@
|
|
10
10
|
|
11
11
|
|
12
12
|
|
13
|
+
def forcast(forecast):
|
14
|
+
|
15
|
+
print("予報日:", forecast["date"])
|
16
|
+
|
17
|
+
print("天気:", forecast["telop"])
|
18
|
+
|
19
|
+
temperature = forecast["temperature"]
|
20
|
+
|
21
|
+
temp_max = temperature["max"]
|
22
|
+
|
23
|
+
temp_min = temperature["min"]
|
24
|
+
|
25
|
+
if temp_max:
|
26
|
+
|
27
|
+
print("最高気温:", temp_max["celsius"] + "℃")
|
28
|
+
|
29
|
+
if temp_min:
|
30
|
+
|
31
|
+
print("最低気温:", temp_min["celsius"] + "℃")
|
32
|
+
|
33
|
+
if temp_max:
|
34
|
+
|
35
|
+
print("最高気温:", temp_max["fahrenheit"] + "°F")
|
36
|
+
|
37
|
+
if temp_min:
|
38
|
+
|
39
|
+
print("最低気温:", temp_min["fahrenheit"] + "°F")
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
|
44
|
+
|
13
45
|
with open("simei.txt") as f:
|
14
46
|
|
15
47
|
lines = f.read().splitlines()
|
@@ -19,6 +51,8 @@
|
|
19
51
|
where = input("天気: ")
|
20
52
|
|
21
53
|
for line in lines:
|
54
|
+
|
55
|
+
print(line)
|
22
56
|
|
23
57
|
if ':' not in line:
|
24
58
|
|
@@ -34,47 +68,15 @@
|
|
34
68
|
|
35
69
|
tenki_data = requests.get(url, params=payload).json()
|
36
70
|
|
37
|
-
|
71
|
+
print(tenki_data["location"]["area"] + "," + tenki_data["title"])
|
38
72
|
|
39
|
-
|
73
|
+
forcast(tenki_data["forecasts"][0])
|
40
74
|
|
41
|
-
|
75
|
+
forcast(tenki_data["forecasts"][1])
|
42
76
|
|
43
|
-
|
77
|
+
print("\n概要:", tenki_data['description']['text'])
|
44
78
|
|
45
|
-
if temperature["max"]:
|
46
|
-
|
47
|
-
w += "\n最高気温:" + temperature["max"]["celsius"] + "℃"
|
48
|
-
|
49
|
-
if temperature["min"]:
|
50
|
-
|
51
|
-
w += "\n最低気温:" + temperature["min"]["celsius"] + "℃"
|
52
|
-
|
53
|
-
if temperature["max"]:
|
54
|
-
|
55
|
-
w += "\n最高気温:" + temperature["max"]["fahrenheit"] + "°F"
|
56
|
-
|
57
|
-
if temperature["min"]:
|
58
|
-
|
59
|
-
w += "\n最低気温:" + temperature["min"]["fahrenheit"] + "°F"
|
60
|
-
|
61
|
-
w += "\n\n予報日:" + tenki_data["forecasts"][1]["date"]
|
62
|
-
|
63
|
-
w += "\n天気:" + tenki_data["forecasts"][1]["telop"]
|
64
|
-
|
65
|
-
w += "\n最高気温:" + tenki_data["forecasts"][1]["temperature"]["max"]["celsius"] + "℃"
|
66
|
-
|
67
|
-
w += "\n最低気温:" + tenki_data["forecasts"][1]["temperature"]["min"]["celsius"] + "℃"
|
68
|
-
|
69
|
-
w += "\n最高気温:" + tenki_data["forecasts"][1]["temperature"]["max"]["fahrenheit"] + "°F"
|
70
|
-
|
71
|
-
w += "\n最低気温:" + tenki_data["forecasts"][1]["temperature"]["min"]["fahrenheit"] + "°F"
|
72
|
-
|
73
|
-
w += "\n\n概要:" + format(tenki_data['description']['text'])
|
74
|
-
|
75
|
-
|
79
|
+
print("\n天気が発表された時間:", tenki_data["publicTime"])
|
76
|
-
|
77
|
-
print(w)
|
78
80
|
|
79
81
|
```
|
80
82
|
|
2
デバッグprint削除
test
CHANGED
@@ -33,8 +33,6 @@
|
|
33
33
|
payload = {"city": city}
|
34
34
|
|
35
35
|
tenki_data = requests.get(url, params=payload).json()
|
36
|
-
|
37
|
-
print(tenki_data)
|
38
36
|
|
39
37
|
w = tenki_data["location"]["area"] + "," + tenki_data["title"]
|
40
38
|
|
1
書き直された質問に対する処理を追加
test
CHANGED
@@ -1,3 +1,91 @@
|
|
1
|
+
「札幌」を入力した場合はこれで動きました。
|
2
|
+
|
3
|
+
最低・最高気温データは None だったので、それを判断する if文が必要でした。
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
```
|
8
|
+
|
9
|
+
import requests
|
10
|
+
|
11
|
+
|
12
|
+
|
13
|
+
with open("simei.txt") as f:
|
14
|
+
|
15
|
+
lines = f.read().splitlines()
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
where = input("天気: ")
|
20
|
+
|
21
|
+
for line in lines:
|
22
|
+
|
23
|
+
if ':' not in line:
|
24
|
+
|
25
|
+
continue
|
26
|
+
|
27
|
+
name, city = line.split(':')
|
28
|
+
|
29
|
+
if where in name:
|
30
|
+
|
31
|
+
url = "http://weather.livedoor.com/forecast/webservice/json/v1"
|
32
|
+
|
33
|
+
payload = {"city": city}
|
34
|
+
|
35
|
+
tenki_data = requests.get(url, params=payload).json()
|
36
|
+
|
37
|
+
print(tenki_data)
|
38
|
+
|
39
|
+
w = tenki_data["location"]["area"] + "," + tenki_data["title"]
|
40
|
+
|
41
|
+
w += "\n予報日:" + tenki_data["forecasts"][0]["date"]
|
42
|
+
|
43
|
+
w += "\n天気:" + tenki_data["forecasts"][0]["telop"]
|
44
|
+
|
45
|
+
temperature = tenki_data["forecasts"][0]["temperature"]
|
46
|
+
|
47
|
+
if temperature["max"]:
|
48
|
+
|
49
|
+
w += "\n最高気温:" + temperature["max"]["celsius"] + "℃"
|
50
|
+
|
51
|
+
if temperature["min"]:
|
52
|
+
|
53
|
+
w += "\n最低気温:" + temperature["min"]["celsius"] + "℃"
|
54
|
+
|
55
|
+
if temperature["max"]:
|
56
|
+
|
57
|
+
w += "\n最高気温:" + temperature["max"]["fahrenheit"] + "°F"
|
58
|
+
|
59
|
+
if temperature["min"]:
|
60
|
+
|
61
|
+
w += "\n最低気温:" + temperature["min"]["fahrenheit"] + "°F"
|
62
|
+
|
63
|
+
w += "\n\n予報日:" + tenki_data["forecasts"][1]["date"]
|
64
|
+
|
65
|
+
w += "\n天気:" + tenki_data["forecasts"][1]["telop"]
|
66
|
+
|
67
|
+
w += "\n最高気温:" + tenki_data["forecasts"][1]["temperature"]["max"]["celsius"] + "℃"
|
68
|
+
|
69
|
+
w += "\n最低気温:" + tenki_data["forecasts"][1]["temperature"]["min"]["celsius"] + "℃"
|
70
|
+
|
71
|
+
w += "\n最高気温:" + tenki_data["forecasts"][1]["temperature"]["max"]["fahrenheit"] + "°F"
|
72
|
+
|
73
|
+
w += "\n最低気温:" + tenki_data["forecasts"][1]["temperature"]["min"]["fahrenheit"] + "°F"
|
74
|
+
|
75
|
+
w += "\n\n概要:" + format(tenki_data['description']['text'])
|
76
|
+
|
77
|
+
w += "\n\n天気が発表された時間:" + tenki_data["publicTime"]
|
78
|
+
|
79
|
+
print(w)
|
80
|
+
|
81
|
+
```
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
最初に書いてあったデータの場合
|
86
|
+
|
87
|
+
|
88
|
+
|
1
89
|
```
|
2
90
|
|
3
91
|
data = [line.strip().split(':') for line in open("data.txt")]
|