Apr-17-2022, 10:07 AM
css is just a text file!
Assuming: $customer_color_1: #cb2324; only appears 1 time, very easy.
Leave out the $, I think that will complicate matters! I think it is a reserved symbol.
Assuming: $customer_color_1: #cb2324; only appears 1 time, very easy.
Leave out the $, I think that will complicate matters! I think it is a reserved symbol.
myfile = '/path/to/mycss_files/my_css_file.css') with open('path2mycssfile') as mycss: mystring = mycss.read() newstring = mystring.replace('customer_color_1: #cb2324', 'customer_color_1: #005f9f') with open('path2mycssfile', 'w') as mycss: mycss.write(newstring)Otherwise, if you know the line number, also very easy.
linenr = 1 with open('path2mycssfile') as mycss: mylist = mycss.readlines() # list numbering starts at 0 # line 1 is mylist[0] my_line = linenr - 1 the_line = mylist[my_line] the_new_line = the_line.replace('customer_color_1: #cb2324', 'customer_color_1: #005f9f') mylist[my_line] = the_new_line mystring = ''.join(mylist) with open('path2mycssfile', 'w') as mycss: mycss.write(mystring)If the text you want to replace is present on more than 1 line, find all the lines with that text, then display them with line number, choose the line you want and replace.