字符串用 R 数据帧的列中的反斜杠和双引号替换字符
String replace character with backslash and double quote in a column of an R dataframe
我有这个数据框:
df <- data.frame(
ID = c("1", "2", "3"),
option_json = c('{"thickness":"0.031 inches","tensile strength":"600 lb","size":"0.5 Inches x 7200 Feet"}', '{"thickness":"0.031 inches","tensile strength":"600 lb","size":"0.5 Inches x 7200 Feet"}' , '{"tensile strength":"600 lb","color":"Black","size":"0.5 Inches x 7200 Feet"}'))
ID option_json
1 1 {"thickness":"0.031 inches","tensile strength":"600 lb","size":"0.5 Inches x 7200 Feet"}
2 2 {"thickness":"0.031 inches","tensile strength":"600 lb","size":"0.5 Inches x 7200 Feet"}
3 3 {"tensile strength":"600 lb","color":"Black","size":"0.5 Inches x 7200 Feet"}
我想要这个数据框:
ID option_json
1 1 {"thickness":"0.031\"","tensile strength":"600 lb","size":"0.5\" x 7200'"}
2 2 {"thickness":"0.031\"","tensile strength":"600 lb","size":"0.5\" x 7200'"}
3 3 {"tensile strength":"600 lb","color":"Black","size":"0.5\" x 7200'"}
我尝试使用 str_replace 和 gsub 来替换英寸,但双引号后面总是出现双反斜杠。不确定如何只用一个反斜杠替换字符串。
我认为在 R 中,如果其中一个反斜杠被转义,它们总是会一起打印两个反斜杠。当两个反斜杠一起显示时,这只是一种语法,表明它们应该被解释为字符“\”而不是转义字符。
为确认这一点,您可以尝试将数据框保存到文本文件中,您会发现字符串中实际上只有一个反斜杠。
df <- df %>% mutate(option_json = gsub(" inches", '\\"', option_json, ignore.case = T) %>%
gsub(" Feet", "\'", ., ignore.case = T))
write.table(df, "df.tsv", quote = F, row.names = F)
从“df.tsv”复制的输出
ID option_json
1 {"thickness":"0.031\"","tensile strength":"600 lb","size":"0.5\" x 7200'"}
2 {"thickness":"0.031\"","tensile strength":"600 lb","size":"0.5\" x 7200'"}
3 {"tensile strength":"600 lb","color":"Black","size":"0.5\" x 7200'"}
尝试打印“option_json”列
可以看到在每个双引号"
字符之前,都有一个转义字符\
。 \
用于表示单个 \
字符。
print(df$option_json)
[1] "{\"thickness\":\"0.031\\"\",\"tensile strength\":\"600 lb\",\"size\":\"0.5\\" x 7200'\"}"
[2] "{\"thickness\":\"0.031\\"\",\"tensile strength\":\"600 lb\",\"size\":\"0.5\\" x 7200'\"}"
[3] "{\"tensile strength\":\"600 lb\",\"color\":\"Black\",\"size\":\"0.5\\" x 7200'\"}"
我有这个数据框:
df <- data.frame(
ID = c("1", "2", "3"),
option_json = c('{"thickness":"0.031 inches","tensile strength":"600 lb","size":"0.5 Inches x 7200 Feet"}', '{"thickness":"0.031 inches","tensile strength":"600 lb","size":"0.5 Inches x 7200 Feet"}' , '{"tensile strength":"600 lb","color":"Black","size":"0.5 Inches x 7200 Feet"}'))
ID option_json
1 1 {"thickness":"0.031 inches","tensile strength":"600 lb","size":"0.5 Inches x 7200 Feet"}
2 2 {"thickness":"0.031 inches","tensile strength":"600 lb","size":"0.5 Inches x 7200 Feet"}
3 3 {"tensile strength":"600 lb","color":"Black","size":"0.5 Inches x 7200 Feet"}
我想要这个数据框:
ID option_json
1 1 {"thickness":"0.031\"","tensile strength":"600 lb","size":"0.5\" x 7200'"}
2 2 {"thickness":"0.031\"","tensile strength":"600 lb","size":"0.5\" x 7200'"}
3 3 {"tensile strength":"600 lb","color":"Black","size":"0.5\" x 7200'"}
我尝试使用 str_replace 和 gsub 来替换英寸,但双引号后面总是出现双反斜杠。不确定如何只用一个反斜杠替换字符串。
我认为在 R 中,如果其中一个反斜杠被转义,它们总是会一起打印两个反斜杠。当两个反斜杠一起显示时,这只是一种语法,表明它们应该被解释为字符“\”而不是转义字符。
为确认这一点,您可以尝试将数据框保存到文本文件中,您会发现字符串中实际上只有一个反斜杠。
df <- df %>% mutate(option_json = gsub(" inches", '\\"', option_json, ignore.case = T) %>%
gsub(" Feet", "\'", ., ignore.case = T))
write.table(df, "df.tsv", quote = F, row.names = F)
从“df.tsv”复制的输出
ID option_json
1 {"thickness":"0.031\"","tensile strength":"600 lb","size":"0.5\" x 7200'"}
2 {"thickness":"0.031\"","tensile strength":"600 lb","size":"0.5\" x 7200'"}
3 {"tensile strength":"600 lb","color":"Black","size":"0.5\" x 7200'"}
尝试打印“option_json”列
可以看到在每个双引号"
字符之前,都有一个转义字符\
。 \
用于表示单个 \
字符。
print(df$option_json)
[1] "{\"thickness\":\"0.031\\"\",\"tensile strength\":\"600 lb\",\"size\":\"0.5\\" x 7200'\"}"
[2] "{\"thickness\":\"0.031\\"\",\"tensile strength\":\"600 lb\",\"size\":\"0.5\\" x 7200'\"}"
[3] "{\"tensile strength\":\"600 lb\",\"color\":\"Black\",\"size\":\"0.5\\" x 7200'\"}"