Skip to content
Advertisement

Python, replacing lines in html file to other

I have html line with gallery:

JavaScript

I need to add 100 more photos in gallery witch are named from 1.jpg to 101.jpg and I do not want to copy paste them one by one, but id rather use python to make it for me. I have got something like this:

JavaScript

But I need to know how to tell Python, to copy our lines 101 times and rewrite every numbers sequentially – from 1 to 101?

Advertisement

Answer

You could use a for loop and create a new string in each loop with the next integer:

JavaScript

The f'{n}.jpg' syntax is a very nice way to include variables into a string. I’m not sure how to parse it into your new html file to save it correctly. Its even possible to put expressions inside the brackets of the formatted string which results in even cleaner code:

JavaScript

For more string formatting info check this link on formatting f-strings.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement