Learn, Share, Build

245
October 11, 2017, at 12:13 PM

So I have some code to make a module and I need help utilizing a for loop with .replace() in it. The problem is that when I loop through variables, and use .replace(k, v), my code will only replace the last key with it's matching last value in the dictionary. This leaves all the previous keys left in the output without being replaced with their matching value... I need every key to be replaced with every value, and it will only comply by doing the last key in the dict

variables = {'font1': 'Harlow Solid', 'grey': '#454545', 'font2': 'mistral'}
style_sheet ='''/* Pyle Sheets */
hashone{
    font: Italic 70px font1;
}
hashtwo{                       # scroll down to see 'for' loop in question
    font: bold 45px font2;     # scroll down to see 'for' loop in question
}
hashthree{
    font: Italic 25px Harlow Solid;
}
dotsample{
    text-align: center;
}
hashbodyid{
    background: grey;
}
'''
replace_num = len(variables.keys()) # <--for loop/.replace(k, v) function
for k, v in variables.items():      # <--for loop/.replace(k, v) function
    content = style_sheet.replace(k, v, replace_num) # <--- here it is...
looped_content = str(content)
id_content = looped_content.replace("hash", "#")
clss_idContent = id_content.replace("dot", ".")
#------------
print(clss_idContent) # view output
Answer 1

You overwrite content again and again in the loop while the source string for the replacement (style_sheet) isn't changed.

Answer 2

There is a logical mistake in your code. Let's analyze your code.

Suppose style_sheet equals to aabbcc, and variables equals to {"a": 1, "b" : 2, "c": 3}.

What will happen during loop?

First loop: content = "aabbcc".replace("a", 1, 3), so content = "11bbcc".

Second loop: content = "aabbcc".replace("b", 2, 3), so content = "aa22cc".

Last loop: content = "aabbcc".replace("c", 3, 3), so content = "aabb33".

So do you notice where is the key point?

You should update original string during every loop too!

You can change your code like the following:

content = style_sheet
for k, v in variables.items():
    content = content.replace(k, v, replace_num)  #update original string
Answer 3

What you could do in the loop is instead of setting it to a variable you could append it to a a list, or do update on a dictionary.

For instance, since looped_content is a string, you could initialize it before the loop and append content to it each time in the loop. Or if it is a dict, update it. This would prevent content from being overwritten each time, as mentioned by another poster.

looped_content = ""

replace_num = len(variables.keys()) # <--for loop/.replace(k, v) function
for k, v in variables.items():      # <--for loop/.replace(k, v) function
    content = style_sheet.replace(k, v, replace_num) # <--- here it is...
    looped_content.append(content)
id_content = looped_content.replace("hash", "#")
clss_idContent = id_content.replace("dot", ".")
Rent Charter Buses Company
READ ALSO
Learn, Share, Build

Learn, Share, Build

I've been trying to figure out how I can change the Opera speed dial color from my site using only CSS

242
Learn, Share, Build

Learn, Share, Build

I'm trying to make a sidebar ease in and out when a button is toggledI have to use JS because I need to use data-binding with Knockout

299
Learn, Share, Build

Learn, Share, Build

I have a floating header that resizes when windows size changesI also have # links that scroll to the corresponding part of the website; the headers shouldnt overlap the destination of the # link

265
Learn, Share, Build

Learn, Share, Build

So I've been messing around a little bit, and continued to inprove my PHP mailsEverything worked good, had my up and downs, and I finally achieved what I wanted to

211