How to save array output as csv file? i've tried with csv module but did not give me the right output. i want the output like the picture bellow.
output1.html
<div class="side-article txt-article">
<p><strong></strong> <a href="http://batam.tribunnews.com/tag/polres/" title="Polres"></a> <a href="http://batam.tribunnews.com/tag/bintan/" title="Bintan"></a></p>
<p><br></p>
<p><a href="http://batam.tribunnews.com/tag/polres/" title="Polres"></a></p>
<p><a href="http://batam.tribunnews.com/tag" title="Polres"></a> <a href="http://batam.tribunnews.com/tag/bintan/" title="Bintan"></a></p>
<br>
i have code :
import csv
from bs4 import BeautifulSoup
from HTMLParser import HTMLParser
with open('output1.html', 'r') as f:
html = f.read()
soup = BeautifulSoup(html.strip(), 'html.parser')
for line in html.strip().split('\n'):
link_words = 0
line_soup = BeautifulSoup(line.strip(), 'html.parser')
for link in line_soup.findAll('a'):
link_words += len(link.text.split())
# naive way to get words count
words_count = len(line_soup.text.split())- link_words
number_tag_p = len(line_soup.find_all('p'))
number_tag_br = len(line_soup.find_all('br'))
number_tag_break = number_tag_br + number_tag_p
#for line in html.strip().split('\n'):
number_of_starttags = 0
number_of_endtags = 0
# create a subclass and override the handler methods
class MyHTMLParser(HTMLParser):
def handle_starttag(self, tag, attrs):
global number_of_starttags
number_of_starttags += 1
def handle_endtag(self, tag):
global number_of_endtags
number_of_endtags += 1
# instantiate the parser and fed it some HTML
parser = MyHTMLParser()
parser.feed(line.lstrip())
number_tag = number_of_starttags + number_of_endtags
#print(number_of_starttags + number_of_endtags)
CTTD = words_count + link_words + number_tag_break
if (words_count + link_words) == 0:
CTTD == 0
else:
CTTD
print ('TC : {0} LTC : {1} TG : {2} P : {3} CTTD : {4}'
.format(words_count, link_words, number_tag, number_tag_break, CTTD))
res = ('TC : {0} LTC : {1} TG : {2} P : {3} CTTD : {4}'
.format(words_count, link_words, number_tag, number_tag_break, CTTD))
csvfile = "./output1.csv"
#Assuming res is a flat list
with open(csvfile, "wb") as output:
writer = csv.writer(output, lineterminator='\n')
for val in res:
writer.writerow([val])
#Assuming res is a list of lists
with open(csvfile, "wb") as output:
writer = csv.writer(output, lineterminator='\n')
writer.writerows(res)
the output of algorithm
TC : 0 LTC : 0 TG : 0 P : 0 CTTD : 0
TC : 0 LTC : 0 TG : 0 P : 0 CTTD : 0
TC : 0 LTC : 0 TG : 1 P : 0 CTTD : 0
TC : 0 LTC : 0 TG : 1 P : 0 CTTD : 0
TC : 15 LTC : 0 TG : 2 P : 0 CTTD : 15
the output csv :
how to save the print to csv? any python library can do this?
i expected the output will be
Thank you.