html指定位置插入文本,python:在文本指定位置插入文本

导读

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

结果

AssemblySample
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
contigs83
Largest contig18538724
Total length22360815
GC (%)32.927
N5018387284
N7518387249
L5010
L7513
N's per 100 kbp0.00

😀😀

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值