英文:
How to replace \" with " in string columns scala
问题
{
"translation": "我有一列格式混乱的嵌套JSON字符串,我正在尝试使用regexp_replace
来编辑它,以便Scala可以将该列读取为一个结构。JSON中的每个项目都添加了随机的\"
,我希望用"
替换它们。\n\n我已经使用regexp_replace
删除了\n
,但我在处理每个项目周围的\"
时遇到了困难。\n\nvar newdf = df.withcolumn( "clean_json", regexp_replace(regexp_replace(col("json"), "\n", ""), """"""", "\""))\n\n我尝试过使用\\\
和"""
作为转义字符,但似乎都不起作用。"
}
英文:
I have a column of badly formatted nested json string that I'm trying to edit with regexp_replace
. So that the column can be read by scala as a struct. There are random \"
added to each item in the Json that i want to replace with "
{
{"id": "1", "json": [
{ \"details\": {\n \"name\" : \"john\", \n \"lastname\" : \"doe"\ \n},
\"location\": {\n \"city\" : \"new york\", \n \"country\" : \"usa\" \n} },
{ \"details\": {\n \"name\" : \"jane\", \n \"lastname\" : \"random"\ \n},
\"location\": {\n \"city\" : \"new york\", \n \"country\" : \"usa\" \n} },
] },
{"id": "2", "json": [
{ \"details\": {\n \"name\" : \"jack\", \n \"lastname\" : \"ryan"\ \n},
\"location\": {\n \"city\" : \"york\", \n \"country\" : \"uk\" \n} },
{ \"details\": {\n \"name\" : \"jill\", \n \"lastname\" : \"test"\ \n},
\"location\": {\n \"city\" : \"LA\", \n \"country\" : \"usa\" \n} },
] }
}
I was able to remove the \n
with regexp_replace
but i'm struggling with the \"
wrapping each item.
var newdf = df.withcolumn( "clean_json",
regexp_replace(regexp_replace(col("json"), "\n", ""), """\"""", "\\\""))
I've tried using both \\\
and """
as escape characters. nothing seems to work
答案1
得分: 0
如果你想替换\
,你应该替换为\\\\
。
df = spark.createDataFrame(['\\"'], StringType()).toDF('value')
df.withColumn('new_value', f.regexp_replace('value', '\\\\"', '')).show(truncate=False)
+-----+---------+
|value|new_value|
+-----+---------+
|\" | |
+-----+---------+
英文:
If you want to replace \
, you should replace with \\\\
.
df = spark.createDataFrame(['\\"'], StringType()).toDF('value')
df.withColumn('new_value', f.regexp_replace('value', '\\\\"', '')).show(truncate=False)
+-----+---------+
|value|new_value|
+-----+---------+
|\" | |
+-----+---------+
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论