回答編集履歴

4

少し編集

2019/01/25 21:16

投稿

namnium1125
namnium1125

スコア2043

test CHANGED
@@ -254,7 +254,7 @@
254
254
 
255
255
 
256
256
 
257
- # 追記
257
+ # 追記3
258
258
 
259
259
 
260
260
 

3

追記

2019/01/25 21:16

投稿

namnium1125
namnium1125

スコア2043

test CHANGED
@@ -251,3 +251,21 @@
251
251
  print "%s :\n%s\n" % (key, str(val))
252
252
 
253
253
  ```
254
+
255
+
256
+
257
+ # 追記
258
+
259
+
260
+
261
+ フォーマット文字列はPython3.6以降の機能でした、明記しておらずすみません。
262
+
263
+ Python3.5以前のバージョンで実行している場合は`format`メソッドを使用してください。
264
+
265
+
266
+
267
+ ```
268
+
269
+ print("{} :\n{}\n".format(key, val))
270
+
271
+ ```

2

追記

2019/01/25 21:15

投稿

namnium1125
namnium1125

スコア2043

test CHANGED
@@ -205,3 +205,49 @@
205
205
 
206
206
 
207
207
  ```
208
+
209
+
210
+
211
+ # 追記2
212
+
213
+
214
+
215
+ 一応、Python2.7系でも動くであろうコードも載せておきますが、私はPython2.7系は詳しくないので、良いコードかどうかは保証しかねます。
216
+
217
+
218
+
219
+ ```python
220
+
221
+ import glob
222
+
223
+
224
+
225
+ matrixs = dict()
226
+
227
+
228
+
229
+ for file in sorted(glob.glob("a/*.csv")):
230
+
231
+ matrixs[file] = []
232
+
233
+ with open(file, 'r') as f:
234
+
235
+ for line in f.read().splitlines():
236
+
237
+ line = line.replace(b'\xef\xbb\xbf', "")
238
+
239
+ line_data_arr = [int(s) for s in line.split(",") if s != ""]
240
+
241
+ if len(line_data_arr) == 4:
242
+
243
+ # print(line_data_arr)
244
+
245
+ matrixs[file].append(line_data_arr)
246
+
247
+
248
+
249
+ for key, val in matrixs.items():
250
+
251
+ print "%s :\n%s\n" % (key, str(val))
252
+
253
+ ```

1

追記

2019/01/25 11:09

投稿

namnium1125
namnium1125

スコア2043

test CHANGED
@@ -137,3 +137,71 @@
137
137
 
138
138
 
139
139
  ```
140
+
141
+
142
+
143
+ # 追記
144
+
145
+
146
+
147
+ 二次元配列にしたいならば例えばこうでしょうか?
148
+
149
+
150
+
151
+ ```python
152
+
153
+ import glob
154
+
155
+
156
+
157
+ matrixs = dict()
158
+
159
+
160
+
161
+ for file in sorted(glob.glob("a/*.csv")):
162
+
163
+ matrixs[file] = []
164
+
165
+ with open(file, 'r', encoding="utf-8-sig") as f: #ファイルを読み取りモードで開く
166
+
167
+ for line in f.read().splitlines():
168
+
169
+ line_data_arr = [int(s) for s in line.split(",") if s != ""]
170
+
171
+ if len(line_data_arr) == 4:
172
+
173
+ # print(line_data_arr)
174
+
175
+ matrixs[file].append(line_data_arr)
176
+
177
+
178
+
179
+ for key, val in matrixs.items():
180
+
181
+ print(f"{key} :\n{val}\n")
182
+
183
+ ```
184
+
185
+
186
+
187
+ 実行結果例
188
+
189
+
190
+
191
+ ```bash
192
+
193
+ $ python3 tera170375.py
194
+
195
+ a\1.csv :
196
+
197
+ [[10, 100, 1000, 10000], [11, 101, 1001, 10001]]
198
+
199
+
200
+
201
+ a\2.csv :
202
+
203
+ [[1, 2, 3, 4], [11, 12, 13, 14]]
204
+
205
+
206
+
207
+ ```