python - 如何在C++中将Python字符串转换为其转义版本?

标签 python c++ escaping

我正在尝试编写一个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中甚至可能吗?

最佳答案

您可以使用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))

关于python - 如何在C++中将Python字符串转换为其转义版本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60133856/

相关文章:

python - 在 Mac Os 中编译和链接 Python 模块

javascript - JQuery/Flask - 将数据以 JSON 形式从服务器发送到浏览器

python - 具有共享 x 轴的 Seaborn 图

java - 使用java代码从文本文件中读取单个反斜杠

python - 如何找到第一层的后代?

c++ - 字符串替换 C++ 中的空格中断

c++ - 如何使用 C++ 和 QtQuick 实现游戏循环

c++ - 为什么我的浮点值看起来不等于它本身?

javascript - 如何替换 JavaScript 中的加号?

windows - 批处理文件中的特殊字符