I'm trying to change a file containing strings like:
Record 1 :
{ "K1":"value1" ,
"K2":"value2"
}
Record 2 :
{ "K1":"value3" ,
"K2":"value4"
}
to
{
"Record_1" :
{ "K1": "value1",
"K2": "value2"
},
"Record_2" :
{ "K1": "value3",
"K2": "value4"
}
}
(to make it into a correct JSON format).
The part of code that I'm having problems with is:
pattern = r"(\s*)Record (\d+):"
all_records_json = re.sub(middle_pattern, "\"Record_"+ ??? + "\" : ",all_records)
And I don't know what to put instead of ??? so that it reads the (\\d+) part that matched the pattern.
First, your pattern doesn't actually match your data in the first place:
>>> all_records = '''Record 2 :
... { "K1":"value3" ,
... "K2":"value4"
... }'''
>>> pattern = r"(\s*)Record (\d+):"
>>> re.findall(pattern, all_records)
[]
That's because your data has a space between the digits and the colon. You need to fix that. While we're at it, I have no idea why you're putting a group around the preceding whitespace, so let's not do that. So we get:
>>> pattern = r"\s*Record (\d+)\s*:"
>>> re.findall(pattern, all_records)
[2]
Now, your only capturing group is the \\d+
. So that will be group 1. Which you can include in the substitution as \\1
. So:
>>> print(re.sub(pattern, r'"Record_\1": ', all_records))
"Record_2":
{ "K1":"value3" ,
"K2":"value4"
}
That still isn't valid JSON, but it's what you wanted, right?
If you read the docs, re.sub
explains that "Backreferences, such as \\6
, are replaced with the substring matched by group 6 in the pattern." For full details on back references for both (…)
groups and (?P<name>…)
groups, look them up in the Regular Expression Syntax . You should also read the Regular Expression HOWTO , which explains all of this in a more novice-friendly way.
Notice that I made the substitution a raw string just like the pattern, so I didn't have to escape the \\1
, and I also used single instead of double quotes, so I didn't have to escape the "
characters.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.