3

I simply want to go through and find every numerical value in a single, or batch of, CSS files and multiple times two, then save.

Any suggestions for the easiest way to do this?

6
  • 3
    Be prepared for a lot of unexpected results. Commented Nov 8, 2011 at 18:36
  • 2
    What are you trying to accomplish? Commented Nov 8, 2011 at 18:46
  • 2
    I don't have a solution to your problem, but to avoid these issues in the future, you can look at using extended stylesheet langues like SASS or LESS. Commented Nov 8, 2011 at 18:48
  • That's a really bizarre question. I guess you can just read a token, check if it's a number and multiply it by two, and write to a file. Commented Nov 9, 2011 at 6:19
  • find all numeric value? you mean the numbers? Commented Nov 9, 2011 at 11:49

1 Answer 1

1

Using regular expressions could solve your problem. For example, in python you could do the following:

import re

input = "#content {width:100px;height:20.5%;font-size:150.25%;margin-left:-20px;padding:2 0 -20 14.33333;}"

regex = re.compile("-?[.0-9]+")
scaled_numbers = [float(n)*2 for n in re.findall(regex, input)]
split_text = re.split(regex, input)

output = ''
for i in range(len(scaled_numbers)):
    output += "%s%.2f" % (split_text[i], scaled_numbers[i])

output += split_text[-1]

This code could be reduced in length, but I've deliberately left it less compact for readability. One flaw with it is that it contracts floats to only 2 decimal places, but that can be easily changed if you really need extended decimals (change the number in "%s%.2f" to the desired number of places).

Note also that this code could change the names of CSS selectors (for example, #footer-2 would become #footer-4.00). If you wanted to avoid that, you'll need to adjust the code to ignore text outside of {...}.

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

Comments

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.