在R中用双引号替换转义的双引号 [英] Replacing escaped double quotes by double quotes in R

查看:311
本文介绍了在R中用双引号替换转义的双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用RMySQL将一些html代码写入SQL数据库(但我猜想我的问题是一个普遍的R问题,而非与SQL或RMySQL相关)。所以我正在尝试这样的东西:

  con < -  RMySQL(...)#some connection 
html < - < div style ='text-align:center; font-family:Arial;'>< span style ='font-size:14pt;'>一些没有任何棘手符号的文本。跨度>< / DIV>中
query< - c('INSERT INTO table(htmlfield)VALUES(\',html,''')
dbSendQuery(con,paste(query,collapse = $ b

麻烦的是,R的粘贴将以单引号(即''')替换为转义序列的双引号\,即:

 >粘贴(query,collapse =)
[1] INTO表(htmlfield)VALUES(\< div style ='text-align:center; font-family:Arial;'>< span style ='font-size:14pt;'& < / span>< / div> \

如果我更改单引号在向量查询中加上双引号,单引号在html中加倍,问题出现在字符串html的一侧,因此html中的双引号将被转义的顺序所取代。



处理转义字符的最简单方法是什么?



我尝试过 gsub('\\\\'',''',html)它没有按预期工作,并且在帖子中建议的解决方案隐藏R字符串中的转义字符(反斜杠),但我无法使其正常工作。

$感谢您的关注,Philipp

解决方案

我看到您所包含的两个问题你的问题。第一个看起来像一个打字错误。之后:

  html<  - < div style ='text-align:center; font-family:Arial; >< span style ='font-size:14pt;'>一些没有任何棘手符号的文本。< / span>< / div> 

您有:

 查询<  -  c('INSERT INTO table(htmlfield)VALUES(\',html,'')
^^^^^^^^^^^^^注意你逃避一个字符串,而不是另一个字符串,你不需要逃避它们,但是没关系,如果你这样做,你也意味着')'的最后一个字符串,我怀疑,你得到的错误的真正来源。 code>粘贴
而不是 c 在这里更有用,如果我结合这些,我们得到:

 查询<  - 粘贴('INSERT INTO表(htmlfield)VALUES(',html,')',sep =)

我们可以直接使用:

 code> dbSendQuery(con,query)

第二个问题, ,是将对象的打印表示与对象本身混淆,如果我们打印 query ,我们看到:

 >查询
[1]INSERT INTO表(htmlfield)VALUES(\< div style ='text-align:center; font-family:Arial;'>< span style ='font-size :14pt;'>一些没有任何棘手符号的文字。< / span>< / div> \)

字符串的打印表示始终包含在双引号中,因此内部需要转义,你想要看的是实际的字符串,我们可以用 cat writeLines - 我喜欢后者,因为它将$ code>\\\
自动添加到字符串的末尾:

 > writeLines(query)
INSERT INTO表(htmlfield)VALUES(< div style ='text-align:center; font-family:Arial; '>< span style ='font-size:14pt;'>一些没有任何棘手符号的文本< / span>< / div>)
注意如何现在不转义。那就是由数据库服务器执行的SQL。如果这是您的数据库的有效SQL,那么它将工作。


I am writing some html code to an SQL databse using RMySQL (but I guess my problem is rather a general R question than really related to SQL or RMySQL). So I am trying something like this:

con <- RMySQL(...) # some connection    
html <- "<div style='text-align: center; font-family: Arial;'><span style='font-size: 14pt;'>Some text without any tricky symbols.</span></div>"    
    query <- c('INSERT INTO table (htmlfield) VALUES (\"', html, '"') 
    dbSendQuery(con,paste(query, collapse = ""))

Trouble is, R's paste will replace the double quotes in single quotes (i.e. '"') to the escaped sequence \", i.e.:

> paste(query, collapse = "")
[1] "INSERT INTO table (htmlfield) VALUES (\"<div style='text-align: center; font-family: Arial;'><span style='font-size: 14pt;'>Some text without any tricky symbols.</span></div>\""

If I change the single quotes in the vector query to double quotes, and the single quotes in html to doubles, the problem is then on the side of the character string html, since then the double quotes in html get replaced by the escaped sequence.

What's the easiest way to handle a substitution of the escaped characters?

I tried gsub('\\\"','"',html) which did not work as intended and the solutions suggested in the post Ignore escape characters (backslashes) in R strings but I could not make it work.

Thanks for your attention, Philipp

解决方案

I see two problems with what you included in your Question. The first looks like a typo. After:

html <- "<div style='text-align: center; font-family: Arial;'><span style='font-size: 14pt;'>Some text without any tricky symbols.</span></div>"   

You have:

query <- c('INSERT INTO table (htmlfield) VALUES (\"', html, '"')
                                                  ^^^^^^^^^^^^^^^

Notice you escape one string but not the other. You don't need to escape them, but it doesn't matter if you do. You also meant '")' for the last string which is, I suspect, the real source of the error you are getting. paste rather than c is more useful here. If I combine these, we get:

query <- paste('INSERT INTO table (htmlfield) VALUES ("', html, '")', sep = "")

that we can use directly:

dbSendQuery(con, query)

The second problem, and one that many people make, is to confuse the printed representation of an object with the object itself. If we print query, we see this:

> query
[1] "INSERT INTO table (htmlfield) VALUES (\"<div style='text-align: center; font-family: Arial;'><span style='font-size: 14pt;'>Some text without any tricky symbols.</span></div>\")"

The printed representation of the string is always enclosed in "" double quotes, and as such the internal " need to be escaped. What you want to look at is the actual string. We can do that with cat or writeLines - I prefer the latter as it adds the "\n" to the end of the string automagically:

> writeLines(query)
INSERT INTO table (htmlfield) VALUES ("<div style='text-align: center; font-family: Arial;'><span style='font-size: 14pt;'>Some text without any tricky symbols.</span></div>")

Notice how the " are now not escaped. That is the SQL that would be executed by the database server. If that is valid SQL for your DB then it will work.

这篇关于在R中用双引号替换转义的双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆