I want to extract characters from the message and save them in json.

Asked 2 months ago, Updated 2 months ago, 1 views

msg="Dictionary registration: Doraemon_Crayon Shin-chan"


  "Doraemon": "Shin-chan Crayon".

In other words, I would like to remove "Dictionary Registration" from the body of the message and save only "Doraemon" and "Crayon Shin-chan" in json. What should I do?


2022-09-30 11:23

2 Answers

I think it's easy to replace with regular expressions.
^Dictionary registration:{\n"$1":"$2"\n}
I think you can replace it with .

Specifically, you can use re in the standard library that handles regular expressions, for example:

import re
msg="Dictionary registration: Doraemon_Crayon Shin-chan"
pat=re.compile(r'^Dictionary registration: (.+)_(.+)$')
# {
# "Doraemon": "Shin-chan Crayon".
# }

2022-09-30 11:23

Dictionary registration: If is a fixed string, you can do the following:

import json

data = {}
data [key] = value

2022-09-30 11:23

If you have any answers or tips

© 2022 OneMinuteCode. All rights reserved.