Python表格存为html

文章目录

  • html表格标签
  • 极简版
  • 详细版(+列名)
  • 更详细版(+跨行)
  • 更更详细版(列数>2)
  • NLP版(NER)

html表格标签

%s\n'%''.join(''.format(i)for i in fields)if fields else'' tr ='\n'.join(''+''.join(''.format(i)for i in ls)+''for ls in ls_of_ls)return'
标签 描述
表格
表格的表头
表格的行
表格单元
表格标题
表格列的组
用于表格列的属性
表格的页眉
表格的主体
表格的页脚

极简版

font size=“5”

def save_html(ls_of_ls, prefix):
    fname = prefix + '.html'
    with open(fname, 'w', encoding='utf-8') as f:
        f.write('\n')for ls in ls_of_ls:
            f.write('')for i in ls:
                f.write(''.format(i))
            f.write('\n')
        f.write('
{}
'
) ls_of_ls = [['笑菊花', '深扣菊花舔指笑,菊花一闪误终身'], ['菊花红', '接天莲叶无穷碧,硬日菊花别样红']] save_html(ls_of_ls, '菊花')

详细版(+列名)

def save_html(table, prefix):
    fname = prefix + '.html'
    with open(fname, 'w', encoding='utf-8') as f:
        f.write('\n')
        f.write(table)
        f.write('')

def make_table(ls_of_ls, fields=None):
    th = '
{}
{}
\n%s\n
' % (th + tr) ls_of_ls = [['笑菊花', '深扣菊花舔指笑,菊花一闪误终身'], ['菊花红', '接天莲叶无穷碧,硬日菊花别样红']] fields = ['title', 'article'] save_html(make_table(ls_of_ls, fields), '菊花')

<html><head><meta charset="UTF-8">head><body><table border="1">
<tr><td><font size="5">笑菊花font>td><td><font size="5">深扣菊花舔指笑,菊花一闪误终身font>td>tr>
<tr><td><font size="5">菊花红font>td><td><font size="5">接天莲叶无穷碧,硬日菊花别样红font>td>tr>
table>body>html>

更详细版(+跨行)

td rowspan=“3”

def save_html(table, prefix):
    fname = prefix + '.html'
    with open(fname, 'w', encoding='utf-8') as f:
        f.write('\n')
        f.write(table)
        f.write('')

def make_table(tb, fields=None):
    th = '%s\n' % ''.join('{}'.format(i) for i in fields) if fields else ''
    return '\n%s\n
'
% (th + tb) def make_tr_by_ls(ls_of_ls): return '\n'.join( '' + ''.join('{}'.format(i) for i in ls) + '' for ls in ls_of_ls) def make_tr_by_dt(dt_of_ls): # tr = '' # for k, v in dt_of_ls.items(): # le = len(v) # for i in range(le): # tr += '' # if i == 0: # tr += '%s' % (le, k) # tr += '%s' % v[i] # tr += '\n' # return tr.strip() return '\n'.join( '%s%s' % ('%s' % (len(v), k) if i == 0 else '', v[i]) for k, v in dt_of_ls.items() for i in range(len(v))) dt_of_ls = {'苹果': ['苹果醋'], '华为': [], '小米': ['小米粥', '小米蛋', '小米肠']} fields = ['title', 'article'] tr = make_tr_by_dt(dt_of_ls) tb = make_table(tr, fields) save_html(tb, '手机')

<html><head><meta charset="UTF-8">head><body><table border="1">
<tr><th>titleth><th>articleth>tr>
<tr><td rowspan="1">苹果td><td>苹果醋td>tr>
<tr><td rowspan="3">小米td><td>小米粥td>tr>
<tr><td>小米蛋td>tr>
<tr><td>小米肠td>tr>
table>body>html>

更更详细版(列数>2)

def save_html(table, prefix):
    fname = prefix + '.html'
    with open(fname, 'w', encoding='utf-8') as f:
        f.write('')
        f.write(table)
        f.write('')

def make_table(tb, fields=None):
    th = '%s\n' % ''.join('{}'.format(i) for i in fields) if fields else ''
    return '\n%s\n
'
% (th + tb) def make_tr_by_dts(dt_of_lss): # tr = '' # for k, v in dt_of_lss.items(): # le = len(v) # for i in range(le): # tr += '' # if i == 0: # tr += '%s' % (le, k) # for j in v[i]: # tr += '%s' % j # tr += '\n' # return tr.strip() return '\n'.join( '%s%s' % ('%s' % (len(v), k) if i == 0 else '', ''.join('%s' % j for j in v[i])) for k, v in dt_of_lss.items() for i in range(len(v))) fields = ['text', 'phrase', 'word'] dt_of_lss = { '用小米机买小米粥和苹果汁': [('小米机', '小米'), ('小米粥', '小米'), ('苹果汁', '苹果')], '买华为送大米': [], '买大米送苹果醋': [('苹果醋', '苹果')], } tr = make_tr_by_dts(dt_of_lss) tb = make_table(tr, fields) save_html(tb, '手机')

Python表格存为html_第1张图片

NLP版(NER)

from jieba import tokenize

replace_html_tag = lambda word: '' + word + ''
replace_word = lambda sentence, word, head, tail: sentence[:head] + word + sentence[tail:]


def save_html(table, prefix):
    fname = prefix + '.html'
    with open(fname, 'w', encoding='utf-8') as f:
        f.write('')
        f.write(table)
        f.write('')


def make_table(tb, fields=None):
    th = '%s\n' % ''.join('{}'.format(i) for i in fields) if fields else ''
    return '\n%s\n
'
% (th + tb) def make_tr_by_dts(dt_of_lss): return '\n'.join( '%s%s' % ('%s' % (len(v), k) if i == 0 else '', ''.join('%s' % j for j in v[i])) for k, v in dt_of_lss.items() for i in range(len(v))) # NLP fields = ['text', 'phrase', 'word'] texts = ['买小米机,送了袋小米和苹果', '诺基亚', '买华为送苹果'] entities = {'小米', '苹果'} dt_of_lss = dict() for text in texts: dt_of_lss[text] = [] for sentence in text.split(','): # 切句 for word, head, tail in tokenize(sentence): # 分词+位置 if word in entities: # NER dt_of_lss[text].append([ replace_word(sentence, replace_html_tag(word), head, tail), word ]) tb = make_table(make_tr_by_dts(dt_of_lss), fields) save_html(tb, '手机')

Python表格存为html_第2张图片

<html><head><meta charset="UTF-8">head><body><table border="1">
<tr><th>textth><th>phraseth><th>wordth>tr>
<tr><td rowspan="3">买小米机,送了袋小米和苹果td><td><font color="red">小米font>td><td>小米td>tr>
<tr><td>送了袋<font color="red">小米font>和苹果td><td>小米td>tr>
<tr><td>送了袋小米和<font color="red">苹果font>td><td>苹果td>tr>
<tr><td rowspan="1">买华为送苹果td><td>买华为送<font color="red">苹果font>td><td>苹果td>tr>
table>body>html>

你可能感兴趣的:(数据处理)