Skip to content

Instantly share code, notes, and snippets.

@shihochan
Last active April 17, 2019 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shihochan/1aaf46f3d94c438379d0e0554a75f298 to your computer and use it in GitHub Desktop.
Save shihochan/1aaf46f3d94c438379d0e0554a75f298 to your computer and use it in GitHub Desktop.
import csv
# 書き込み用ファイルオープン
wf = open('mult99-1.csv', 'w')
writer = csv.writer(wf, lineterminator='\n')
# 例として 1x1 ~ 3x3 までを想定
for i in range(1, 4):
for j in range(1, 4):
file = '{0}x{1}.dat'.format(i, j)
# 読み込み用ファイルオープン
rf = open(file, 'r')
reader = csv.reader(rf)
list1 = []
for row in reader:
# 例:'N1:3\n' => ['N1', '3\n']
list2 = row[0].split(':')
# 改行を取り除いてリストに追加
list1.append(list2[1].rstrip('\n'))
# 例:['3', '2', '6']
writer.writerow(list1)
rf.close()
wf.close
@shihochan
Copy link
Author

shihochan commented Apr 16, 2019

実行結果

➜  ~ python mult99-1.py
➜  ~ tail mult99-1.csv
1,1,1
1,2,2
1,3,3
2,1,1
2,2,4
2,3,6
3,1,3
3,2,6
3,3,9

@shihochan
Copy link
Author

shihochan commented Apr 16, 2019

入力ファイル: 1x1.dat

N1:1
N2:1
ANS:1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment