json.dumps 和 {json.dumps}
The issue is that you’re creating a Python set with the curly braces {}
instead of just storing the JSON string directly:
lyrc_complete_json_str = {json.dumps(lrc, ensure_ascii=False, indent=4)}
When you put the JSON string inside curly braces {}
, Python interprets it as creating a set with one element (the JSON string). That’s why you’re seeing the escaped newlines (\n
) and other formatting characters in the output.
To fix this, simply remove the curly braces:
lyrc_complete_json_str = json.dumps(lrc, ensure_ascii=False, indent=4)
This will store the properly formatted JSON string directly in the variable, without wrapping it in a set, and should resolve the issue with the escaped newlines when the variable is printed or used.
lyrc_complete_json_str 变为了一个json字符串
注意
- 如果吧这个 \n的字符串给到一个python 的字符串做format用,会报错啊