Created
March 21, 2024 14:00
-
-
Save luis-c465/6c748e40b5c1b8899f5d1502228a5cd6 to your computer and use it in GitHub Desktop.
Replaces exponential form of numbers in an svg with their decimal format, `3e-5` => `0.00003`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import decimal | |
import re | |
INPUT_FILE = "input.svg" | |
OUTPUT_FILE = "output.svg" | |
DECIMAL_DIGITS = 10 | |
REGEX = r"[0-9.]+e-\d+" | |
# https://regex101.com/r/9SaiS7/1 | |
data = "" | |
with open(INPUT_FILE, "r") as f: | |
data = f.read() | |
decimal.getcontext().prec = DECIMAL_DIGITS | |
def str_to_val(match): | |
m = match.group() | |
return str(decimal.Decimal(m)) | |
# The number of ending digits the decimal will contain | |
output = re.sub(REGEX, str_to_val, data) | |
with open(OUTPUT_FILE, "w") as f: | |
f.write(output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment