質問編集履歴

2

コードの間違い修正

2015/12/02 11:34

投稿

AudioStakes
AudioStakes

スコア41

test CHANGED
File without changes
test CHANGED
@@ -60,11 +60,11 @@
60
60
 
61
61
  # 置換リストをCSVファイルで読み込む
62
62
 
63
- words_filename = u"replace_words"
63
+ words_filename = 'replace_words'
64
64
 
65
- document_in = u"yokohama_topo.json"
65
+ document_in = 'yokohama.topojson'
66
66
 
67
- document_out = u"yokohama_topo_out.json"
67
+ document_out = 'yokohama_topo_out.json'
68
68
 
69
69
  words = []
70
70
 
@@ -102,7 +102,7 @@
102
102
 
103
103
 
104
104
 
105
- #text = u""
105
+ text = u""
106
106
 
107
107
  with open(document_in, 'rU') as docfile:
108
108
 

1

pythonプログラミング内容アップデート

2015/12/02 11:34

投稿

AudioStakes
AudioStakes

スコア41

test CHANGED
File without changes
test CHANGED
@@ -38,11 +38,103 @@
38
38
 
39
39
 
40
40
 
41
+ 使用しているpythonのプログラムは以下になります。
42
+
43
+ ```ここに言語を入力
44
+
45
+ #coding:utf-8
46
+
47
+ '''
48
+
49
+ Created on 2014/03/27
50
+
51
+
52
+
53
+ @author: hitsuji
54
+
55
+ '''
56
+
57
+ import csv
58
+
59
+
60
+
61
+ # 置換リストをCSVファイルで読み込む
62
+
63
+ words_filename = u"replace_words"
64
+
65
+ document_in = u"yokohama_topo.json"
66
+
67
+ document_out = u"yokohama_topo_out.json"
68
+
69
+ words = []
70
+
71
+
72
+
73
+ def unicode_csv_reader(unicode_csv_data, dialect=csv.excel, **kwargs):
74
+
75
+ # csv.py doesn't do Unicode; encode temporarily as UTF-8:
76
+
77
+ csv_reader = csv.reader(utf_8_encoder(unicode_csv_data),dialect=dialect, **kwargs)
78
+
79
+ for row in csv_reader:
80
+
81
+ # decode UTF-8 back to Unicode, cell by cell:
82
+
83
+ yield [unicode(cell, 'utf-8') for cell in row]
84
+
85
+
86
+
87
+ def utf_8_encoder(unicode_csv_data):
88
+
89
+ for line in unicode_csv_data:
90
+
91
+ yield line.encode('utf-8')
92
+
93
+
94
+
95
+ with open(words_filename + '.csv', 'rU') as csvfile:
96
+
97
+ spamreader = unicode_csv_reader(csvfile, dialect=csv.excel)
98
+
99
+ for row in spamreader:
100
+
101
+ words.append(row)
102
+
103
+
104
+
105
+ #text = u""
106
+
107
+ with open(document_in, 'rU') as docfile:
108
+
109
+ tmpdoc = docfile.read()
110
+
111
+ docfile.close()
112
+
113
+ text = tmpdoc.encode('utf-8')
114
+
115
+
116
+
117
+ for pair in words:
118
+
119
+ text = text.replace(pair[0], pair[1])
120
+
121
+
122
+
123
+ with open(document_out, 'w') as f:
124
+
125
+ f.write(text)
126
+
127
+ f.close()
128
+
129
+ ```
130
+
131
+
132
+
41
133
  それぞれのファイルの中身は以下のようになっています。
42
134
 
43
135
 
44
136
 
45
- ☆JSON(TopoJSON)ファイルの中身
137
+ ☆JSON(TopoJSON)ファイルの中身 (軽く1万文字は超えますが、省略します)
46
138
 
47
139
 
48
140
 
@@ -62,7 +154,7 @@
62
154
 
63
155
 
64
156
 
65
- ☆CSVファイルの中身
157
+ ☆CSVファイルの中身 (200ほどこのような列が続きます)
66
158
 
67
159
  ```ここに言語を入力
68
160