回答編集履歴

2

コードの編集

2019/02/18 23:03

投稿

mackerel6.023
mackerel6.023

スコア317

test CHANGED
@@ -32,7 +32,7 @@
32
32
 
33
33
 
34
34
 
35
- queries = ['赤羽駅']
35
+ queries = []
36
36
 
37
37
  with open(r'C:\work\test.txt', 'r') as f:
38
38
 

1

コードの追記

2019/02/18 23:03

投稿

mackerel6.023
mackerel6.023

スコア317

test CHANGED
@@ -1,3 +1,67 @@
1
1
  入力ファイルのC:\work\test.txtがSJISになっていませんか?
2
2
 
3
3
  UTF-8でファイルを作成したところ動作しました。
4
+
5
+
6
+
7
+ 2019/02/19 8:01追記
8
+
9
+ ファイル出力にしたコードを載せておきます。
10
+
11
+ URLのアスタリスクは実際には有効なAPIキーが入ります。
12
+
13
+ ```Python
14
+
15
+ # -*- coding: utf-8 -*-
16
+
17
+
18
+
19
+ import requests
20
+
21
+ import json
22
+
23
+ from time import sleep
24
+
25
+
26
+
27
+ wait_time = 0.3 # (sec)
28
+
29
+ base_url = 'https://maps.googleapis.com/maps/api/geocode/json?language=ja&address={}&key=***'
30
+
31
+ headers = {'content-type': 'application/json'}
32
+
33
+
34
+
35
+ queries = ['赤羽駅']
36
+
37
+ with open(r'C:\work\test.txt', 'r') as f:
38
+
39
+ queries = f.readlines()
40
+
41
+ for query in queries:
42
+
43
+ query = query.strip()
44
+
45
+ url = base_url.format(query)
46
+
47
+ r = requests.get(url, headers=headers)
48
+
49
+ data = r.json()
50
+
51
+ if 'results' in data and len(data['results']) > 0 and 'formatted_address' in data['results'][0]:
52
+
53
+ with open(r'C:\work\test_out.txt', 'a') as out:
54
+
55
+ out.write('{}@{}@{}@{}'.format(
56
+
57
+ query,
58
+
59
+ data['results'][0]['formatted_address'].encode('utf-8'),
60
+
61
+ data['results'][0]['geometry']['location']['lat'],
62
+
63
+ data['results'][0]['geometry']['location']['lng']
64
+
65
+ ))
66
+
67
+ ```