【发布时间】:2018-07-21 12:56:51
【问题描述】:
我正在用我修改过的 CSS 字符串创建一个字符串。 修改后的结果是这样的:
.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 }'
我当然不需要那些空格和'\'。 我得到的最接近的是这样做:
translator = str.maketrans('', '', ' \\n\t\r')
changed_css_as_string =str(sheet.cssText).translate(translator)
输出如下:
.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}'
哪个更接近,但主要问题是不再有“n”。没有现在是oe。那不好。我该怎么做才能获得所需的输出?
【问题讨论】:
-
嗨! :) 请提供更多代码!开头提到的字符串的生成会很有趣。你知道函数
.replace('old', 'new')吗?像'old'.replace('old', 'new').replace('new', 'blah')或str.replace('\n', '')一样删除换行符。如果这还不够,您还可以使用 RegEx。
标签: python regex string replace translation