【发布时间】:2021-04-05 15:06:09
【问题描述】:
我的期望不正确吗? 所需的输出是针对单个字段的
"{\"escapeChar\":\"\\\", \"quoteChar\":\"\"\", \"separatorChar\":\",\"}"
输入 csv 文本
"{""escapeChar"":""\\"", ""quoteChar"":""\"""", ""separatorChar"":"",""}"
代码:
obj_str = json.dumps(table_obj).replace('RS Target Table', 'Table Name').replace('Stage Table', 'input_table')
#Get obj back with replacement BANG!!
obj = json.loads(obj_str)
print ('after loads, source_serde_params: ', obj['source_serde_params'])
with open(filename, 'w' ) as write_file:
json.dump(obj, write_file, indent=2)
控制台:
after loads, source_serde_params: {"escapeChar":"\\", "quoteChar":"\"", "separatorChar":","}
输出文件:
"source_serde_params": "{\"escapeChar\":\"\\\\\", \"quoteChar\":\"\\\"\", \"separatorChar\":\",\"}"
python 3.7.9 笔记本电脑
【问题讨论】:
-
为什么将 JSON 放入 CSV 文件中?
-
您想要的输出不是有效的 json。第一个 \ 将转义 " 并弄乱 json 的其余部分。此外,前导和尾随 " 不是有效 json 的一部分。
-
@RaniSharim,OP 希望他们的 JSON 文档由单个字符串组成。这样解释是有效的。