-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtools.py
executable file
·72 lines (56 loc) · 1.81 KB
/
tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import io
import argparse
def count_lines(path):
with io.open(path, encoding='utf-8', errors='ignore') as f:
return sum([1 for _ in f])
def make_dictionary(text):
output = {}
text_list = text.split("\n")
for i in range(len(text_list)):
output[i+1] = text_list[i]
return output
def insert_char(text):
text.replace(".\n", " .\n")
def write_row(text, num):
output = ""
outlist = []
text_dict = make_dictionary(text)
for k in text_dict.keys():
if k == num+1:
break
else:
outlist.append(text_dict[k])
# output += "\n"
output = '\n'.join(outlist)
return output
def textfile_io(ifile_name, ofile_name):
"""
main
input and output
call some functions that have process you want to do
"""
# read text data
with io.open(ifile_name, encoding="utf-8") as f:
text_data = f.read()
f.close()
result = ""
######################################################
# result = tools_filewriting.insert_char(text_data)
result = write_row(text_data, 5000000)
######################################################
# write result
with io.open(ofile_name, "w", encoding="utf-8") as f:
f.write(result)
f.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
prog = 'Character filter',
usage = 'python tools.py [filename]',
description = '',
epilog = 'end',
add_help = True,
)
parser.add_argument("-i", '--ifilename', help='file name of input text file')
parser.add_argument("-o", "--ofilename", help='file name of output text file')
args = parser.parse_args()
textfile_io(args.ifilename, args.ofilename)