Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,28 @@
|
|||
csvtxt = '''\
|
||||
Character,Speech
|
||||
The multitude,The messiah! Show us the messiah!
|
||||
Brians mother,<angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry>
|
||||
The multitude,Who are you?
|
||||
Brians mother,I'm his mother; that's who!
|
||||
The multitude,Behold his mother! Behold his mother!\
|
||||
'''
|
||||
|
||||
from cgi import escape
|
||||
|
||||
def _row2tr(row, attr=None):
|
||||
cols = escape(row).split(',')
|
||||
return ('<TR>'
|
||||
+ ''.join('<TD>%s</TD>' % data for data in cols)
|
||||
+ '</TR>')
|
||||
|
||||
def csv2html(txt):
|
||||
htmltxt = '<TABLE summary="csv2html program output">\n'
|
||||
for rownum, row in enumerate(txt.split('\n')):
|
||||
htmlrow = _row2tr(row)
|
||||
htmlrow = ' <TBODY>%s</TBODY>\n' % htmlrow
|
||||
htmltxt += htmlrow
|
||||
htmltxt += '</TABLE>\n'
|
||||
return htmltxt
|
||||
|
||||
htmltxt = csv2html(csvtxt)
|
||||
print(htmltxt)
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
<TABLE summary="csv2html program output">
|
||||
<TBODY><TR><TD>Character</TD><TD>Speech</TD></TR></TBODY>
|
||||
<TBODY><TR><TD>The multitude</TD><TD>The messiah! Show us the messiah!</TD></TR></TBODY>
|
||||
<TBODY><TR><TD>Brians mother</TD><TD><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></TD></TR></TBODY>
|
||||
<TBODY><TR><TD>The multitude</TD><TD>Who are you?</TD></TR></TBODY>
|
||||
<TBODY><TR><TD>Brians mother</TD><TD>I'm his mother; that's who!</TD></TR></TBODY>
|
||||
<TBODY><TR><TD>The multitude</TD><TD>Behold his mother! Behold his mother!</TD></TR></TBODY>
|
||||
</TABLE>
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
def _row2trextra(row, attr=None):
|
||||
cols = escape(row).split(',')
|
||||
attr_tr = attr.get('TR', '')
|
||||
attr_td = attr.get('TD', '')
|
||||
return (('<TR%s>' % attr_tr)
|
||||
+ ''.join('<TD%s>%s</TD>' % (attr_td, data) for data in cols)
|
||||
+ '</TR>')
|
||||
|
||||
def csv2htmlextra(txt, header=True, attr=None):
|
||||
' attr is a dictionary mapping tags to attributes to add to that tag'
|
||||
|
||||
attr_table = attr.get('TABLE', '')
|
||||
attr_thead = attr.get('THEAD', '')
|
||||
attr_tbody = attr.get('TBODY', '')
|
||||
htmltxt = '<TABLE%s>\n' % attr_table
|
||||
for rownum, row in enumerate(txt.split('\n')):
|
||||
htmlrow = _row2trextra(row, attr)
|
||||
rowclass = ('THEAD%s' % attr_thead) if (header and rownum == 0) else ('TBODY%s' % attr_tbody)
|
||||
htmlrow = ' <%s>%s</%s>\n' % (rowclass, htmlrow, rowclass[:5])
|
||||
htmltxt += htmlrow
|
||||
htmltxt += '</TABLE>\n'
|
||||
return htmltxt
|
||||
|
||||
htmltxt = csv2htmlextra(csvtxt, True,
|
||||
dict(TABLE=' border="1" summary="csv2html extra program output"',
|
||||
THEAD=' bgcolor="yellow"',
|
||||
TBODY=' bgcolor="orange"'
|
||||
)
|
||||
)
|
||||
print(htmltxt)
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
<TABLE border="1" summary="csv2html extra program output">
|
||||
<THEAD bgcolor="yellow"><TR><TD>Character</TD><TD>Speech</TD></TR></THEAD>
|
||||
<TBODY bgcolor="orange"><TR><TD>The multitude</TD><TD>The messiah! Show us the messiah!</TD></TR></TBODY>
|
||||
<TBODY bgcolor="orange"><TR><TD>Brians mother</TD><TD><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></TD></TR></TBODY>
|
||||
<TBODY bgcolor="orange"><TR><TD>The multitude</TD><TD>Who are you?</TD></TR></TBODY>
|
||||
<TBODY bgcolor="orange"><TR><TD>Brians mother</TD><TD>I'm his mother; that's who!</TD></TR></TBODY>
|
||||
<TBODY bgcolor="orange"><TR><TD>The multitude</TD><TD>Behold his mother! Behold his mother!</TD></TR></TBODY>
|
||||
</TABLE>
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
from csv import DictReader
|
||||
from xml.etree import ElementTree as ET
|
||||
|
||||
def csv2html_robust(txt, header=True, attr=None):
|
||||
# Use DictReader because, despite what the docs say, reader() doesn't
|
||||
# return an object with .fieldnames
|
||||
# (DictReader expects an iterable that returns lines, so split on \n)
|
||||
reader = DictReader(txt.split('\n'))
|
||||
|
||||
table = ET.Element("TABLE", **attr.get('TABLE', {}))
|
||||
thead_tr = ET.SubElement(
|
||||
ET.SubElement(table, "THEAD", **attr.get('THEAD', {})),
|
||||
"TR")
|
||||
tbody = ET.SubElement(table, "TBODY", **attr.get('TBODY', {}))
|
||||
|
||||
if header:
|
||||
for name in reader.fieldnames:
|
||||
ET.SubElement(thead_tr, "TD").text = name
|
||||
|
||||
for row in reader:
|
||||
tr_elem = ET.SubElement(tbody, "TR", **attr.get('TR', {}))
|
||||
|
||||
# Use reader.fieldnames to query `row` in the correct order.
|
||||
# (`row` isn't an OrderedDict prior to Python 3.6)
|
||||
for field in reader.fieldnames:
|
||||
td_elem = ET.SubElement(tr_elem, "TD", **attr.get('TD', {}))
|
||||
td_elem.text = row[field]
|
||||
|
||||
return ET.tostring(table, method='html')
|
||||
|
||||
htmltxt = csv2html_robust(csvtxt, True, {
|
||||
'TABLE': {'border': "1", 'summary': "csv2html extra program output"},
|
||||
'THEAD': {'bgcolor': "yellow"},
|
||||
'TBODY': {'bgcolor': "orange"}
|
||||
})
|
||||
|
||||
print(htmltxt.decode('utf8'))
|
||||
|
|
@ -0,0 +1 @@
|
|||
<TABLE border="1" summary="csv2html extra program output"><THEAD bgcolor="yellow"><TR><TD>Character</TD><TD>Speech</TD></TR></THEAD><TBODY bgcolor="orange"><TR><TD>The multitude</TD><TD>The messiah! Show us the messiah!</TD></TR><TR><TD>Brians mother</TD><TD><angry>Now you listen here! He's not the messiah; he's a very naughty boy! Now go away!</angry></TD></TR><TR><TD>The multitude</TD><TD>Who are you?</TD></TR><TR><TD>Brians mother</TD><TD>I'm his mother; that's who!</TD></TR><TR><TD>The multitude</TD><TD>Behold his mother! Behold his mother!</TD></TR></TBODY></TABLE>
|
||||
Loading…
Add table
Add a link
Reference in a new issue