【问题标题】:How to convert a Python string into its escaped version in C++?如何在 C++ 中将 Python 字符串转换为其转义版本?
【发布时间】:2024-01-09 02:02:01
【问题描述】:

我正在尝试编写一个 Python 程序,该程序读取文件并将内容打印为单个字符串,因为它将以 C++ 格式进行转义。这是因为字符串将从 Python 输出中复制并粘贴到 C++ 程序中(C++ 字符串变量定义)。

基本上,我想转换

<!DOCTYPE html>
<html>
<style>
.card{
    max-width: 400px;
     min-height: 250px;
     background: #02b875;
     padding: 30px;
     box-sizing: border-box;
     color: #FFF;
     margin:20px;
     box-shadow: 0px 2px 18px -4px rgba(0,0,0,0.75);
}
</style>

<body>
<div class="card">
  <h4>The ESP32 Update web page without refresh</h4><br>
  <h1>Sensor Value:<span id="ADCValue">0</span></h1><br>
</div>
</body>

<script>
setInterval(function() {
  // Call a function repetatively with 0.1 Second interval
  getData();
}, 100); //100mSeconds update rate

function getData() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      document.getElementById("ADCValue").innerHTML =
      this.responseText;
    }
  };
  xhttp.open("GET", "readADC", true);
  xhttp.send();
}
</script>
</html>

到这里

<!DOCTYPE html>\n<html>\n<style>\n.card{\n    max-width: 400px;\n     min-height: 250px;\n     background: #02b875;\n     padding: 30px;\n     box-sizing: border-box;\n     color: #FFF;\n     margin:20px;\n     box-shadow: 0px 2px 18px -4px rgba(0,0,0,0.75);\n}\n</style>\n\n<body>\n<div class=\"card\">\n  <h4>The ESP32 Update web page without refresh</h4><br>\n  <h1>Sensor Value:<span id=\"ADCValue\">0</span></h1><br>\n</div>\n</body>\n\n<script>\nsetInterval(function() {\n  // Call a function repetatively with 0.1 Second interval\n  getData();\n}, 100); //100mSeconds update rate\n\nfunction getData() {\n  var xhttp = new XMLHttpRequest();\n  xhttp.onreadystatechange = function() {\n    if (this.readyState == 4 && this.status == 200) {\n      document.getElementById(\"ADCValue\").innerHTML =\n      this.responseText;\n    }\n  };\n  xhttp.open(\"GET\", \"readADC\", true);\n  xhttp.send();\n}\n</script>\n</html>

使用这个 Python 程序:

if __name__ == '__main__':
    with open(<filepath>) as html:
        contents = html.read().replace('"', r'\"')

    print(contents)
    print('')
    print(repr(contents))

当“转义”双引号时,我得到的正是我想要的减去双反斜杠。我尝试了一些随机的东西,但所有尝试要么去掉两个反斜杠,要么根本不改变字符串。

我只想在字符串中的所有双引号之前添加一个反斜杠。这在 Python 中是否可行?

【问题讨论】:

  • 使用 C++11 的原始字符串文字会容易得多。事实上,整个问题都消失了。 :)
  • 您需要先将所有\替换为\\,然后然后将“替换为\”。如果你想一想,你就会明白为什么。
  • 你是什么意思“减去双反斜杠”?你是说你也想逃避反斜杠吗?换行符呢?
  • @SamVarshavchik - 我对你的方法有疑问,你能提供一个简单的例子吗?
  • @tdelaney - 不,我不想逃避反斜杠。事实上,这正是我试图在字符串中撤消的内容。我只需要找到一种方法在我的字符串中的每个双引号前插入一个且只有一个反斜杠

标签: python c++ escaping


【解决方案1】:

您可以使用str.translate 将麻烦的字符映射到它们的转义等效项。由于 python 对转义和引号字符的规则可能有点巴洛克式,为了保持一致性,我只是粗暴地强制它们。

# escapes for C literal strings
_c_str_trans = str.maketrans({"\n": "\\n", "\"":"\\\"", "\\":"\\\\"})

if __name__ == '__main__':
    with open(<filepath>) as html:
        contents = html.read().translate(_c_str_trans)

    print(contents)
    print('')
    print(repr(contents))

【讨论】:

  • 使用您的代码,我得到了换行符和双引号的双反斜杠。我需要字符串只包含所有“转义”字符的单个反斜杠转义。还有其他想法吗?
  • @LukeBergeron 您应该在print(contents) 中获得正确的内容,并在print(repr(contents)) 中获得双重转义的内容。第一个看起来对吗?
  • 是的,第一个是完美的——我只看最后一个打印输出。谢谢!
最近更新 更多