From 5ec73980cbe94f932606b5c7dc4bece5a31e248b Mon Sep 17 00:00:00 2001 From: Hikaru Morita Date: Sat, 2 Mar 2019 22:39:03 +0900 Subject: [PATCH] =?UTF-8?q?12=20=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kumbikumbiSIC/12.py | 30 ++++++++++++++++++++ kumbikumbiSIC/12_incomplete.py | 50 ++++++++++++++++++++++++++++++++++ kumbikumbiSIC/col1.txt | 24 ++++++++++++++++ kumbikumbiSIC/col2.txt | 24 ++++++++++++++++ 4 files changed, 128 insertions(+) create mode 100644 kumbikumbiSIC/12_incomplete.py create mode 100644 kumbikumbiSIC/col1.txt create mode 100644 kumbikumbiSIC/col2.txt diff --git a/kumbikumbiSIC/12.py b/kumbikumbiSIC/12.py index e69de29..4b246ea 100644 --- a/kumbikumbiSIC/12.py +++ b/kumbikumbiSIC/12.py @@ -0,0 +1,30 @@ +''' +1列目をcol1.txtに,2列目をcol2.txtに保存 +各行の1列目だけを抜き出したものをcol1.txtに,2列目だけを抜き出したものをcol2.txtとしてファイルに保存せよ.確認にはcutコマンドを用いよ. + +参考 12.pyの答え  : https://qiita.com/segavvy/items/51a515c19bcd29b13b7f +参考 open()について : https://note.nkmk.me/python-file-io-open-with/ +参考 明示的な改行 : https://www.glamenv-septzen.net/view/185 +参考 ジェネレータについて(先頭の関数について) : https://qiita.com/tomotaka_ito/items/15b5999c76001dbc9a58 + : https://qiita.com/tomotaka_ito/items/35f3eb108f587022fa09 +''' + +file_name = "hightemp.txt" + +with open(file_name) as file, open("col1.txt", "w") as col1, \ + open("col2.txt", "w") as col2: + + lines = file.readlines(); + + for line in lines: + cols = line.split("\t") + col1.write(cols[0] + "\n") + col2.write(cols[1] + "\n") + + +''' +cut 参考url +http://x68000.q-e-d.net/~68user/unix/pickup?cut + + +''' \ No newline at end of file diff --git a/kumbikumbiSIC/12_incomplete.py b/kumbikumbiSIC/12_incomplete.py new file mode 100644 index 0000000..778d3b3 --- /dev/null +++ b/kumbikumbiSIC/12_incomplete.py @@ -0,0 +1,50 @@ +''' +1列目をcol1.txtに,2列目をcol2.txtに保存 +各行の1列目だけを抜き出したものをcol1.txtに,2列目だけを抜き出したものをcol2.txtとしてファイルに保存せよ.確認にはcutコマンドを用いよ. + +参考 12.pyの答え  : https://qiita.com/segavvy/items/51a515c19bcd29b13b7f +参考 open()について : https://note.nkmk.me/python-file-io-open-with/ +参考 明示的な改行 : https://www.glamenv-septzen.net/view/185 +参考 ジェネレータについて(先頭の関数について) : https://qiita.com/tomotaka_ito/items/15b5999c76001dbc9a58 + : https://qiita.com/tomotaka_ito/items/35f3eb108f587022fa09 +''' + +class ReiteratableWrapper(object): + def __init__(self, f): + self._f = f + + def __iter__(self): + return self._f() + +file_name = "hightemp.txt" + +with open(file_name) as file, open("col1.txt", "w") as col1, \ + open("col2.txt", "w") as col2: + + lines = file.read(); + print(type(lines)) + + # lines = ReiteratableWrapper(lines) + # linesもう一度ループ文で使いたかった + # イテレータ、ジェンレータが原因か? + + print('col1への書き込み') + for line in lines: + cols = line.split("\t") + #print(cols[0]) + col1.write(cols[0] + "\n") + + # 何も書き込まれない + print('col2への書き込み') + for line in lines: + cols = line.split("\t") + #print(cols[1]) + col2.write(cols[1] + "\n") + + +''' +cut 参考url +http://x68000.q-e-d.net/~68user/unix/pickup?cut + + +''' \ No newline at end of file diff --git a/kumbikumbiSIC/col1.txt b/kumbikumbiSIC/col1.txt new file mode 100644 index 0000000..1ff11e8 --- /dev/null +++ b/kumbikumbiSIC/col1.txt @@ -0,0 +1,24 @@ +高知県 +埼玉県 +岐阜県 +山形県 +山梨県 +和歌山県 +静岡県 +山梨県 +埼玉県 +群馬県 +群馬県 +愛知県 +千葉県 +静岡県 +愛媛県 +山形県 +岐阜県 +群馬県 +千葉県 +埼玉県 +大阪府 +山梨県 +山形県 +愛知県 diff --git a/kumbikumbiSIC/col2.txt b/kumbikumbiSIC/col2.txt new file mode 100644 index 0000000..612ac3a --- /dev/null +++ b/kumbikumbiSIC/col2.txt @@ -0,0 +1,24 @@ +江川崎 +熊谷 +多治見 +山形 +甲府 +かつらぎ +天竜 +勝沼 +越谷 +館林 +上里見 +愛西 +牛久 +佐久間 +宇和島 +酒田 +美濃 +前橋 +茂原 +鳩山 +豊中 +大月 +鶴岡 +名古屋