Skip to content

Instantly share code, notes, and snippets.

@ksomemo
Last active August 11, 2017 00:54
Show Gist options
  • Save ksomemo/fc9b9bbe06633c902202 to your computer and use it in GitHub Desktop.
Save ksomemo/fc9b9bbe06633c902202 to your computer and use it in GitHub Desktop.
ファイルをM行を分割して処理
import itertools
file_line_num = 3
if __name__ == '__main__':
for i in range(1, 10):
with open("test.txt") as f:
start = file_line_num * (i - 1)
stop = file_line_num * i
for l in itertools.islice(f, start, stop):
print(i, start, stop, l.strip())
def func_improve(input_file_name):
with open(input_file_name) as f:
group_key_func = lambda x: x[0] // file_line_num
for key, grouper in itertools.groupby(enumerate(f), group_key_func):
writefile = str(key) + '.txt'
with open(writefile, "w") as out:
out.write('header\n')
for line_num, line in grouper:
out.write(str(line_num) + ':deco_' + line.strip() + '__lated\n')
out.write('footer\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment