diff --git a/chap02/col1.txt b/chap02/col1.txt new file mode 100644 index 0000000..1ff11e8 --- /dev/null +++ b/chap02/col1.txt @@ -0,0 +1,24 @@ +高知県 +埼玉県 +岐阜県 +山形県 +山梨県 +和歌山県 +静岡県 +山梨県 +埼玉県 +群馬県 +群馬県 +愛知県 +千葉県 +静岡県 +愛媛県 +山形県 +岐阜県 +群馬県 +千葉県 +埼玉県 +大阪府 +山梨県 +山形県 +愛知県 diff --git a/chap02/col2.txt b/chap02/col2.txt new file mode 100644 index 0000000..612ac3a --- /dev/null +++ b/chap02/col2.txt @@ -0,0 +1,24 @@ +江川崎 +熊谷 +多治見 +山形 +甲府 +かつらぎ +天竜 +勝沼 +越谷 +館林 +上里見 +愛西 +牛久 +佐久間 +宇和島 +酒田 +美濃 +前橋 +茂原 +鳩山 +豊中 +大月 +鶴岡 +名古屋 diff --git a/chap02/q13.py b/chap02/q13.py new file mode 100644 index 0000000..e460a58 --- /dev/null +++ b/chap02/q13.py @@ -0,0 +1,8 @@ +# coding: utf-8 + +with open("col1.txt", "r") as col1_f,\ + open("col2.txt", "r") as col2_f,\ + open("col12.txt", "w") as col12_f: + for col1, col2 in zip(col1_f, col2_f): + print(col1.rstrip(), col2.rstrip(), sep="\t", file=col12_f) +# paste col1.txt col2.txt diff --git a/chap02/q14.py b/chap02/q14.py new file mode 100644 index 0000000..2bf3651 --- /dev/null +++ b/chap02/q14.py @@ -0,0 +1,14 @@ +# coding:utf-8 +import sys + +args = sys.argv +n = int(args[1]) +path = args[2] + +with open(path, "r") as fin: + for idx, line in enumerate(fin): + if n <= idx: + break + print(line, end="") +# python q14.py 4 col1.txt +# head -n 4 col1.txt diff --git a/chap02/q15.py b/chap02/q15.py new file mode 100644 index 0000000..95569d0 --- /dev/null +++ b/chap02/q15.py @@ -0,0 +1,13 @@ +# coding:utf-8 + +import sys +from collections import deque + +args = sys.argv +n = int(args[1]) +path = args[2] + +with open(path, "r") as fin: + print(*deque(fin, n), sep="", end="") +# python q15.py 4 col1.txt +# tail -n 4 col1.txt diff --git a/chap02/q16.py b/chap02/q16.py new file mode 100644 index 0000000..04606e7 --- /dev/null +++ b/chap02/q16.py @@ -0,0 +1,33 @@ +# coding:utf-8 + +import sys +from itertools import islice + + +def count_file_lines(f): + for i, line in enumerate(f): + pass + f.seek(0) + return i + 1 + + +def devide_num(dividend, divisor): + quotient = dividend // divisor + remainer = dividend % divisor + return (quotient + 1,) * remainer + (quotient,) * (divisor - remainer) + + +if __name__ == '__main__': + args = sys.argv + n = int(args[1]) + path = args[2] + with open(path, "r") as f: + total_line_num = count_file_lines(f) + l = devide_num(total_line_num, n) + for i, line_num in enumerate(l): + with open("{}.{}".format(path, i), "w") as fout: + for line in islice(f, line_num): + print(line, end="", file=fout) + +# python q16.py 5 col1.txt +# split -l 5 col1.txt diff --git a/chap02/q17.py b/chap02/q17.py new file mode 100644 index 0000000..e1056a6 --- /dev/null +++ b/chap02/q17.py @@ -0,0 +1,11 @@ +# coding:utf-8 + +import fileinput + + +if __name__ == '__main__': + prefectures = {line.split("\t")[0] for line in fileinput.input()} + print(*prefectures, sep="\n") + +# python q17.py hightemp.txt +# cut -f1 hightemp.txt |sort|uniq