如何在C++;? 我试图编写一个Python程序,它读取文件并打印内容作为一个字符串,因为它将以C++格式逃脱。这是因为字符串将从Python输出复制并粘贴到C++程序(C++字符串变量定义)中。
基本上,我想转换如何在C++;? 我试图编写一个Python程序,它读取文件并打印内容作为一个字符串,因为它将以C++格式逃脱。这是因为字符串将从Python输出复制并粘贴到C++程序(C++字符串变量定义)中。,python,c++,escaping,Python,C++,Escaping,基本上,我想转换 <!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 18p
<!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>
.卡片{
最大宽度:400px;
最小高度:250px;
背景:#02b875;
填充:30px;
框大小:边框框;
颜色:#FFF;
利润率:20px;
盒影:0px28px-4pxRGBA(0,0,0,0.75);
}
ESP32在不刷新的情况下更新网页
传感器值:0
setInterval(函数(){
//以0.1秒的间隔重复调用函数
getData();
}, 100); //100毫秒更新率
函数getData(){
var xhttp=newXMLHttpRequest();
xhttp.onreadystatechange=函数(){
if(this.readyState==4&&this.status==200){
document.getElementById(“ADCValue”).innerHTML=
这个.responseText;
}
};
xhttp.open(“GET”,“readADC”,true);
xhttp.send();
}
对此
<!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>
\n\n\n.card{\n最大宽度:400px;\n最小高度:250px;\n背景:#02b875;\n填充:30px;\n框大小:边框框;\n颜色:#FFF;\n边距:20px;\n框阴影:0px 2px 18px-4px rgba(0,0,0,0.75);\n}\n\n\n\n\n\n\n\n\n\n\n\n\n在不刷新的情况下更新ESP32网页的传感器值:0
\n\n\n\n{\n//以0.1秒的间隔重复调用函数\n getData();\n},100)//100毫秒更新率\n\n函数getData(){\n var xhttp=new XMLHttpRequest();\n xhttp.onreadystatechange=function(){\n如果(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
使用此Python程序:
if __name__ == '__main__':
with open(<filepath>) as html:
contents = html.read().replace('"', r'\"')
print(contents)
print('')
print(repr(contents))
如果uuuu name_uuuu=='\uuuuuuu main\uuuuuu':
使用open()作为html:
contents=html.read().replace(“”,r“\”)
印刷品(目录)
打印(“”)
打印(报告(内容))
当“转义”双引号时,我得到的正是我想要的减去两个反斜杠。我尝试了一些随机的东西,但是所有的尝试要么去掉两个反斜杠,要么根本不改变字符串
我只想在字符串中的所有双引号之前添加一个反斜杠。这在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))
#C文字字符串的转义
_c\u str\u trans=str.maketrans({“\n”:“\\n”、“\\”:“\\\”、“\\\”:“\\\\”})
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
使用open()作为html:
contents=html.read().translate(_c_str_trans)
印刷品(目录)
打印(“”)
打印(报告(内容))
使用C++11的原始字符串文本会容易得多。事实上,整个问题都消失了您需要首先将所有\替换为\,然后才将“替换为\”。如果你想一想,一分钟后,你就会明白为什么。你说的“减去两个反斜杠”是什么意思?你是说你也想避开反斜杠吗?新词呢?@SamVarshavchik-我对你的方法有疑问,你能举个简单的例子吗?@tdelaney-不,我不想逃避反斜杠。事实上,这正是我试图在字符串中撤消的内容。我只需要找到一种方法,在我的字符串中的每个双引号前面插入一个且仅一个反斜杠。使用您的代码,我得到了换行符和双引号的双反斜杠。我需要字符串只包含所有“转义”字符的单个反斜杠转义。还有其他想法吗?@LukeBergeron你应该在print(contents)
中获得正确的内容,在print(repr(contents))
中获得双转义内容。第一张看起来对吗?是的,第一张很完美-我只是在看最后一张打印出来的。谢谢