0

I am trying to parse CSS file to change style fo html. Look at the example CSS text as follow :

p.FM_table_cell_body308 {
      margin-left:0.000pt;
      margin-right:0.000pt;
      text-align:left;
      text-indent:0.000pt;
}

p.FM_table_cell_body309 {
      margin-left:0.000pt;
      margin-right:0.000pt;
      text-align:left;
      text-indent:0.000pt;
}

p.FM_table_cell_body {
      margin-left:0.000pt;
      margin-right:0.000pt;
      text-align:left;
      text-indent:0.000pt;
}

I want to change the word 'left' of 'text-align:left;' to 'center' in every under 'p.FM_table_cell_body*** {' (*** is number).

So, this is what i am looking foward as follow :

p.FM_table_cell_body308 {
      margin-left:0.000pt;
      margin-right:0.000pt;
      text-align:center;
      text-indent:0.000pt;
}

p.FM_table_cell_body309 {
      margin-left:0.000pt;
      margin-right:0.000pt;
      text-align:center;
      text-indent:0.000pt;
}

p.FM_table_cell_body {   # this has no number so should pass
      margin-left:0.000pt;
      margin-right:0.000pt;
      text-align:left;   # no change
      text-indent:0.000pt;
}

And it's what i am coding so far as below :

from bs4 import BeautifulSoup
import glob2  
import re

with open ("C:\\TEST\\HTML\\Output_sample1\\Responsive HTML5\\Output.css","r",encoding="utf-8") as file_css :
    with open ("C:\\TEST\\HTML\\Output_sample1\\Responsive HTML5\\Output_test.css","wt",encoding="utf-8") as file_css_text_align :
        lines_css = file_css.readlines()
        for i_css, line_css in enumerate(lines_css[:-1]):
            try :
                FM_number_css = re.findall('FM_table_cell_body[1-9][0-9]?[0-9]?[0-9]?', line_css)
                text_align_content = lines_css[i_css + 3]
                if FM_number_css and text_align_content == 'text-align:left;':
                    text_align_content = text_align_content.split(':')
                    text_align_content.remove('left;\n')
                    text_align_content.insert(1, 'center;\n')
                    new_text_align_content = ':'.join(text_align_content)

                    file_css_text_align.write(str(new_text_align_content))
                 else :
                     file_css_text_align.write(str(line_css))
             except : 
                 pass

but this code i am trying to make doesn't work right. this code changes not this(text-align:left;) but (p.FM_table_cell_body*** {) to text-align:center; .

Would you guys give me any advice for me ?

Thanks have a great day.

1 Answer 1

1

You can use a single regular expression with re.sub: search for

(p\.FM_table_cell_body\d+ {[^}]+text-align:)left

to capture everything in a section, up to text-align: in a group, then match 'left', and replace with the first captured group plus 'center', thereby replacing 'left's with 'center's:

https://regex101.com/r/r0dDBz/1

input = '''p.FM_table_cell_body308 {
      margin-left:0.000pt;
      margin-right:0.000pt;
      text-align:left;
      text-indent:0.000pt;
}

p.FM_table_cell_body309 {
      margin-left:0.000pt;
      margin-right:0.000pt;
      text-align:left;
      text-indent:0.000pt;
}

p.FM_table_cell_body {
      margin-left:0.000pt;
      margin-right:0.000pt;
      text-align:left;
      text-indent:0.000pt;
}'''
print(re.sub(r'(p\.FM_table_cell_body\d+ {[^}]+text-align:)left', r'\1center', input))
Sign up to request clarification or add additional context in comments.

1 Comment

Wow! what the heck... this is really good for real. Thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.