-
-
Save ksomemo/fc9b9bbe06633c902202 to your computer and use it in GitHub Desktop.
ファイルをM行を分割して処理
This file contains hidden or 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
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