Randomizer
Randomizer

Reputation: 505

Translator on a string in python issue

I am creating a string out of a css string that I modified. After the modification the results is this:

.cls-24 {\n    fill: lime;\n    opacity: 0.5\n    }\n.cls-25 {\n    fill: none;\n    stroke: #333;\n    stroke-miterlimit: 10;\n    stroke-width: 35px\n    }\n.cls-26 {\n    fill: #333\n    }\n.cls-27 {\n    fill: #7f6145\n    }\n.cls-28 {\n    opacity: 0.2\n    }'

I of course do not need those white spaces and the '\'. The closest I got was doing this:

    translator = str.maketrans('', '', ' \\n\t\r')
    changed_css_as_string =str(sheet.cssText).translate(translator)

And the output is as follows:

.cls-24{fill:lime;opacity:0.5}.cls-25{fill:oe;stroke:#333;stroke-miterlimit:10;stroke-width:35px}.cls-26{fill:#333}.cls-27{fill:#7f6145}.cls-28{opacity:0.2}'

Which is closer but the main issue is that there are no 'n' anymore. the none is now oe. That's no good. What can I do in order to get the desired output?

Upvotes: 1

Views: 44

Answers (2)

Randomizer
Randomizer

Reputation: 505

Ok so I figured it out, this did the trick:

translator = str.maketrans('', '', ' \t\r')
css_without_newline_chars = str(sheet.cssText).replace('\\n', '')
changed_css_as_string = css_without_newline_chars.translate(translator)

Upvotes: 1

qräbnö
qräbnö

Reputation: 3031

Remove the backslash \ before \n!

Wrong

translator = str.maketrans('', '', ' \\n\t\r')

Remove five chars: , \, n, \t and \r

Fixed

translator = str.maketrans('', '', ' \n\t\r')

Remove four chars: , \n, \t and \r

Upvotes: 1

Related Questions