列表数据转前端数据
# _*_ coding:utf-8 _*_
# program infos:表格数据 转化成前端格式
def row_reduce(table, keywords):
"""
合并行
:param table:
:return:
"""
ret = []
for words in table:
lt = []
tmp = words[-1]
num = 1
for word in words[-2::-1]:
if tmp == word and word not in keywords:
num += 1
# daxiang 标记这是个合并项,后面会跳过
lt.append('daxiang_row_')
continue
line = tmp + '_row_' + str(num)
lt.append(line)
tmp = word
num = 1
line = tmp + '_row_' + str(num)
lt.append(line)
lt = lt[::-1]
ret.append(lt)
return ret
def col_reduce(table, keywords):
"""
纵向合并
:param table:
:param keywords:
:return:
"""
if not table:
return table
max_col_num = len(table)
new_table = [[] for i in range(max_col_num)]
max_row_num = len(table[0])
if max_col_num == 1:
words = table[0]
for i in range(max_row_num):
word = words[i] + '_col_1'
new_table[0].append(word)
return new_table
# 多行,转置
temp_table = table[::-1]
tmp_words = temp_table[0]
for row_num in range(max_row_num):
tmp_word = tmp_words[row_num]
ct = 1
for col_num in range(1, max_col_num):
word = temp_table[col_num][row_num]
t_word = word.split('_row_')[0]
if tmp_word == word and t_word not in keywords:
# 说明有列方向有数据一样
new_word = 'daxiang_row_col'
new_table[col_num - 1].append(new_word)
ct += 1
continue
new_word = tmp_word + '_col_%d' % ct
new_table[col_num - 1].append(new_word)
ct = 1
tmp_word = word
new_word = tmp_word + '_col_%d' % ct
new_table[max_col_num - 1].append(new_word)
new_table = new_table[::-1]
return new_table
def get_html(table, keywords):
"""
将表格数据转化为html字符串
:param table:
:return:
"""
text = "<table border='1'>"
for words in table:
line = '<tr>'
for word in words:
lt = word.split('_row_')
value = lt[0]
if value == 'daxiang':
continue
lt1 = lt[1].split('_col_')
if value in keywords:
row_num = lt1[0]
col_num = '1'
else:
row_num = lt1[0]
col_num = lt1[1]
if row_num != '1' and row_num:
line += "<td colspan='%s' " % row_num
else:
line += '<td '
if col_num != '1':
line += " rowspan='%s'>" % col_num
else:
line += '>'
line += value + '</td>'
line += '</tr>'
text += line
text += '</table>'
text = text.replace('\n', '').replace('\t', '')
return text
def start(table):
keywords = ['', '【】', '-', '--', '指']
table = row_reduce(table, keywords)
table = col_reduce(table, keywords)
table_str = get_html(table, keywords)
return table_str
keywords里面都是一些特殊的占位符,生成的字符串,放入到xxx.html文件中就可以打开查看效果了。
前端数据转列表数据
def get_table(table_str):
table = table_str.replace("<table border='1'>", '').replace('</table>', '').replace('<tr>', '')
row_num = table.count('</tr>')
trs = table.split('</tr>')[:-1]
head_tr = trs[0]
col_pattern = 'colspan=\'(\d+)\''
row_pattern = "rowspan='(\d+)'"
value_pattern = '>(.*?)<'
col_num = head_tr.count('</td>')
if 'colspan' in head_tr:
nums = re.findall(col_pattern, head_tr)
mp = map(int, nums)
col_num = sum(mp) + col_num - len(nums)
dp = [[None] * col_num for _ in range(row_num)]
for i in range(row_num):
tr = trs[i]
tds = tr.split('/td>')[:-1]
col_index = 0
for td in tds:
values = re.findall(value_pattern, td)
if values:
value = values[0]
else:
value = ''
while dp[i][col_index] != None:
col_index += 1
if '<td >' in td:
dp[i][col_index] = value
if 'rowspan' in td and 'colspan' not in td:
nums = re.findall(row_pattern, td)
mp = list(map(int, nums))
for k in range(i, i + mp[0]):
dp[k][col_index] = value
elif 'colspan' in td and 'rowspan' not in td:
nums = re.findall(col_pattern, td)
mp = list(map(int, nums))
for k in range(col_index, col_index + mp[0]):
dp[i][k] = value
elif 'colspan' in td and 'rowspan' in td:
r_nums = re.findall(row_pattern, td)
r_mp = list(map(int, r_nums))
r_num = r_mp[0]
c_nums = re.findall(col_pattern, td)
c_mp = list(map(int, c_nums))
c_num = c_mp[0]
for m in range(r_num):
for k in range(c_num):
dp[i + m][col_index + k] = value
col_index += 1
return dp