1

Given two files A and B, is there a way to edit the font, colour etc of the strings in B that overlap with a string in A when matching the two files? The strings that don't match should be left as they were, so the output files should remain the same as the input with regard to length.

Example:

file 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

file B

 NM_134083
 NM_177305
 NM_17343052324

Output

 **NM_134083**  mmu-miR-96-5p   **NM_134083**       0.96213 -0.054
 **NM_177305**  mmu-miR-96-5p   **NM_177305**       0.95707 -0.099
user3741035
  • 2,455
  • 4
  • 15
  • 20

1 Answers1

1

You give raw text and don't specify the kind of formatting you want to do. Leaving the formatting details out, yes you can replace text in FileA that is also in FileB with formatted content.

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)

output

**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
user3885927
  • 3,363
  • 2
  • 22
  • 42