Created
November 14, 2016 07:52
-
-
Save elnikkis/3f4d098eba0d96b33a0217f192795bd1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
''' | |
入力ファイルをn分割する。 | |
出力ファイルはdデフォルトでsplitted0~splitted[n] | |
''' | |
import sys | |
def parse_args(): | |
import argparse | |
parser = argparse.ArgumentParser(description='Split a file into pieces') | |
parser.add_argument('-n', help='split into n parts', | |
type=int, required=True) | |
parser.add_argument('input', type=argparse.FileType('r'), default=sys.stdin, | |
help='input file (default: stdin)') | |
parser.add_argument('--prefix', help='prefix of output files', | |
default='splitted') | |
return parser.parse_args() | |
if __name__ == '__main__': | |
args = parse_args() | |
data = args.input.readlines() | |
length = len(data) | |
part = length / args.n | |
print(length) | |
for i in range(args.n): | |
start = round(part * i) | |
end = round(part * (i + 1)) | |
print(start, '<= x <', end) | |
with open('{}{}'.format(args.prefix, i), 'w') as outfile: | |
p = data[start:end] | |
outfile.writelines(p) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment