Python Forum
Is it possible to update a CSS file from Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is it possible to update a CSS file from Python
#2
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.

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.
bigAL_python likes this post
Reply


Messages In This Thread
RE: Is it possible to update a CSS file from Python - by Pedroski55 - Apr-17-2022, 10:07 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to read csv file update matplotlib column chart regularly SamLiu 2 1,120 Jan-21-2023, 11:33 PM
Last Post: SamLiu
  value null when update in json file 3lnyn0 6 3,460 Dec-30-2021, 05:52 PM
Last Post: ndc85430
  |SOLVED] Glob JPGs, read EXIF, update file timestamp? Winfried 5 2,571 Oct-21-2021, 03:29 AM
Last Post: buran
Question Python + Google Sheet | Best way to update specific cells in a single Update()? Vokofe 1 2,744 Dec-16-2020, 05:26 AM
Last Post: Vokofe
  Update Python Files Harshil 3 2,316 Aug-28-2020, 10:52 AM
Last Post: Harshil
  Config file update Olivier74 0 1,514 Aug-18-2020, 03:36 PM
Last Post: Olivier74
  update txt file but keep a specific line 3Pinter 2 2,142 Dec-16-2019, 07:54 AM
Last Post: 3Pinter
  Update any file Evil_Patrick 1 1,470 Sep-24-2019, 04:26 PM
Last Post: Larz60+
  XML update using python sathiyarajmca 1 2,145 Jun-18-2019, 06:11 PM
Last Post: Larz60+
  Python csv compare two file, update value if two value is matched kinojom 1 2,580 Apr-17-2019, 10:36 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020