gpt4 book ai didi

python - 匹配两个文件之间的行并标记匹配的字符串

转载 作者:太空宇宙 更新时间:2023-11-03 18:10:57 24 4
gpt4 key购买 nike

给定两个文件 A 和 B,有没有办法在匹配这两个文件时编辑 B 中与 A 中的字符串重叠的字符串的字体、颜色等?不匹配的字符串应保留原样,因此输出文件的长度应与输入保持相同。

示例:

文件A

 NM_134083  mmu-miR-96-5p   NM_134083       0.96213 -0.054
NM_177305 mmu-miR-96-5p NM_177305 0.95707 -0.099
NM_026184 mmu-miR-93-3p NM_026184 0.9552 -0.01

文件B

 NM_134083
NM_177305
NM_17343052324

输出

 **NM_134083**  mmu-miR-96-5p   **NM_134083**       0.96213 -0.054
**NM_177305** mmu-miR-96-5p **NM_177305** 0.95707 -0.099

最佳答案

您提供原始文本,但不指定您想要执行的格式设置类型。忽略格式详细信息,是的,您可以用格式化内容替换 FileA 中也​​在 FileB 中的文本。

import re
with open('fileA.txt') as A:
A_content=[x.strip() for x in A]
with open('fileB.txt') as B:
B_content=[x.strip() for x in B]
output=[]
for line_A in A_content:
for line_B in B_content:
#do whatever formatting you need on the text,
# I am just surrounding it with *'s here

replace = "**" + line_B + "**"

#use re.sub,
# details here: https://docs.python.org/2/library/re.html#re.sub

line_A = re.sub(line_B, replace , line_A)
#I am adding everything to the output array but you can check if it is
# different from the initial content. I leave that for you to do
output.append(line_A)

输出

**NM_134083**  mmu-miR-96-5p   **NM_134083**       0.96213 -0.054
**NM_177305** mmu-miR-96-5p **NM_177305** 0.95707 -0.099
NM_026184 mmu-miR-93-3p NM_026184 0.9552 -0.01

关于python - 匹配两个文件之间的行并标记匹配的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25959543/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com