-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathkfile.py
58 lines (47 loc) · 1.52 KB
/
kfile.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
"""
kfile.py
File related library will be saved.
Note
====
k---.py files do not import rdkit since they are common files.
"""
import pickle
def save_obj(obj, name):
with open(name + '.pkl', 'wb') as f:
pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL)
def load_obj(name):
with open(name + '.pkl', 'rb') as f:
return pickle.load(f)
def remove_returns_in_a_phragraph(fname_read, fname_write=None, disp=False):
"""
I will read a text file and remove carriage returns in each line unless
it is blank line. Then, the modified will be saved back.
"""
with open(fname_read, "r") as f:
lines = f.readlines()
lines_mod = list()
line_mod = ""
for line in lines:
if line != '\n':
# If it is a part of a paragraph, it will be concatenated with the previous parts.
line_mod += line[:-1] + ' '
else:
if len(line_mod) > 0:
if disp:
print(line_mod)
print()
lines_mod.append(line_mod + '\n')
lines_mod.append('\n')
line_mod = ""
with open( fname_write, "w") as f:
print("The modified text is saved to", fname_write)
f.writelines( lines_mod)
def cat(fname):
"""
cat(fname)
print the content of the file of fname like cat in system
"""
print()
with open(fname, 'r') as f:
for s in f.readlines():
print(s, end='')