2

This is probobly a blink of an eye to most of you but I am learning.

I am editing multiple web pages and want to edit one line at the top and insert a value into it with an item in a list.

Then move onto the next item in the list and edit the template file with this item and move onto the next one. I have read for a few hours on here now but cannot find a solution. Please can someone help.

Currently have a file structure of: websitetemplate.asp (there is a line at the top with "SKU" in it which is the portion to edit. sku_list.txt (this has multiple values which I want to insert into the template and save each one.

5
  • I am helping you. Not that hard. If you use UNIX I can give you one short command to do that Commented Feb 19, 2021 at 13:23
  • are you using windows or linux? Commented Feb 19, 2021 at 13:24
  • Using windows 10 but have access to UNIX. I was hoping to use Python if possible because I am trying to teach myself it. Commented Feb 19, 2021 at 13:29
  • You can combine UNIX commands with python. Just try : Commented Feb 19, 2021 at 13:32
  • du -a | awk '{print $2}' | grep '\.html$' can return all your html files. with python you can make a list of all your files and use ed to append that Commented Feb 19, 2021 at 13:34

1 Answer 1

3

Here's a quick python solution:

with open('sku_list.txt', 'r') as skusfile:
    for sku in skusfile.read().split('\n'):
        with open("websitetemplate.asp", 'r') as templatefile:
            template = templatefile.read()
        with open(f"{sku}.asp", 'w+') as writefile:
            writefile.write(template.replace('[replacekeyword]', sku))

It opens your list and splits it by newlines (insert whatever operator you have inbetween).

Then it opens the website template and saves it in the template variable.

Last, you replace whatever replacekeyword you have with the different values in the skulist and write them each to their own file.

Sign up to request clarification or add additional context in comments.

10 Comments

Thank you for this. The output is close to what I am after - it creates the new files names as the SKU but it doesn't change the value SKU within the file - does that make sense?
If it is what you were looking for, please accept the answer :)
How can I edit the string in 'website template' which is current reads 'SKU' with the same value as the name for that page? Otherwise I have lots of different named pages but they will all be identical.
You replace [replacekeyword] with SKU
writefile.write(template.replace('SKU', sku))
|

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.