导读
read文本后用re.sub替换即可。
一、输入文件
html框架
table1
AssemblySamplecontigs (>= 0 bp)112contigs (>= 1000 bp)62contigs (>= 5000 bp)53contigs (>= 10000 bp)54contigs (>= 25000 bp)45contigs (>= 50000 bp)36Total length (>= 0 bp)22731637Total length (>= 1000 bp)22292735Total length (>= 5000 bp)22266586Total length (>= 10000 bp)22266569Total length (>= 25000 bp)22047235Total length (>= 50000 bp)21658865contigs83Largest contig18538724Total length22360815GC (%)32.927N5018387284N7518387249L5010L7513N's per 100 kbp0.00二、整个方法
脚本
temple:html框架
table_id:插入位置【被替换的,位置信息,unique】
infile:html表格
outfile:临时结果文件【被替换的结果】
为了起到保存而不是另存为的效果,以此方法。本打算用temple即作为输入有作为输出的,结果原文件都被清空了,open一个文件后,在close前不能再次open。
#!/usr/bin/env python3
import sys, re, os
ms, temple, table_id, infile, outfile= sys.argv
with open(temple) as f_html:
with open(infile) as f_table:
f_html = f_html.read()
f_table = f_table.read()
judge = f_html.find(table_id)
if judge != -1:
result = re.sub(table_id, f_table, f_html)
with open(outfile, 'w') as o:
o.write(result)
else:
print("error: there is no " + table_id)
# 最后用新html代替旧html
if judge != -1:
os.remove(temple)
os.rename(outfile, temple)
三、运行结果
运行
python3 table_insert_html.py test.html table1 test.tsv temp.txt
结果
Assembly | Sample |
---|---|
contigs (>= 0 bp) | 112 |
contigs (>= 1000 bp) | 62 |
contigs (>= 5000 bp) | 53 |
contigs (>= 10000 bp) | 54 |
contigs (>= 25000 bp) | 45 |
contigs (>= 50000 bp) | 36 |
Total length (>= 0 bp) | 22731637 |
Total length (>= 1000 bp) | 22292735 |
Total length (>= 5000 bp) | 22266586 |
Total length (>= 10000 bp) | 22266569 |
Total length (>= 25000 bp) | 22047235 |
Total length (>= 50000 bp) | 21658865 |
contigs | 83 |
Largest contig | 18538724 |
Total length | 22360815 |
GC (%) | 32.927 |
N50 | 18387284 |
N75 | 18387249 |
L50 | 10 |
L75 | 13 |
N's per 100 kbp | 0.00 |
😀😀