-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml_parser.py
executable file
·41 lines (31 loc) · 1.13 KB
/
html_parser.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
#!/home/dd/anaconda3/bin/python
# HTML formatting tool for powercasts output files
import sys
def url_list_to_htm(link_list, filename=False):
"""takes list of urls, optionally filename to write to,
returns numbered HTML links"""
links = []
for i, line in enumerate(link_list):
url = line.strip()
# build HTML link string
html = '#' + str(i) + ' ' + '<a href="' + url + '">' + url + '</a>'
links.append(html)
if filename:
with open(filename, 'w') as f:
for line in links:
f.write(line)
f.write(" <br> ")
print('Wrote file ' + filename)
return(links)
def read_file_to_list(filename):
"""read content of file into list (internal)"""
with open(filename, 'r') as f:
content = f.readlines()
return(content)
if __name__ == '__main__':
# check for number of arguments
if len(sys.argv) == 3:
file_content = read_file_to_list(sys.argv[1])
result = url_list_to_htm(file_content, sys.argv[2])
else:
print('mandatory arguments [filename to read] [filename to write]')