Чтобы упростить мою проблему, у меня есть база в json, и я восстанавливаю все свои строки json, чтобы поместить информацию в базу. Временами это кажется легким, но проблема в том, что мой json написан неправильно
Так что я сделал код для восстановления всех моих json строк, но он не работает на всех строках, например " биография ie ".
я показываю вам
{"name": "Nazamiu0304 Rau0304majiu0304", "personal_name": "Nazamiu0304 Rau0304majiu0304", "last_modified": {"type": "/type/datetime", "value": "2008-08-20T18:00:41.270799"}, "key": "/authors/OL1001461A", "type": {"key": "/type/author"}, "revision": 2}
{"name": "Nazamiu0304 Rau0304majiu0304", "personal_name": "Nazamiu0304 Rau0304majiu0304", "last_modified": {"type": "/type/datetime", "value": "2008-08-20T18:00:41.270799"}, "key": "/authors/OL1001461A", "type": {"key": "/type/author"}, "revision": 2}
видите, у вас есть имя, личное имя ...
иногда у вас есть другая информация,
{"bio": {"type": "/type/text", "value": "> "Eversley, William Pinder, B.C.L. Queen's Coll., Oxon, M.A., a member of the South-eastern circuit, reporter for Law Times in Queen's Bench division, a student of the Inner Temple 14 April, 1874 (then aged 23), called to the bar 25 April, 1877 (eldest son of William Eversley, Esq., of London); born u2060, 1851. rn> rn> 7, King's Bench Walk, Temple, E.C." rn> ...[in Foster's _Men at the Bar_][1]rnrnrn rnrn[1]: https://en.wikisource.org/wiki/Men-at-the-Bar/Eversley,_William_Pinder "Men at the Bar""}, "name": "William Pinder Eversley", "created": {"type": "/type/datetime", "value": "2008-04-01T03:28:50.625462"}, "death_date": "1918", "photos": [6897255, 6897254], "last_modified": {"type": "/type/datetime", "value": "2018-07-31T15:39:07.982159"}, "latest_revision": 6, "key": "/authors/OL1003081A", "birth_date": "1851", "personal_name": "William Pinder Eversley", "type": {"key": "/type/author"}, "revision": 6}
{"name": "Valerie Meyer", "personal_name": "Valerie Meyer", "last_modified": {"type": "/type/datetime", "value": "2008-08-20T18:22:33.63997"}, "key": "/authors/OL1004062A", "type": {"key": "/type/author"}, "revision": 2}
Вы можете видеть, что у меня много проблем с элементом "био": поскольку он вообще не написан правильно, квота не интерпретируется корректно, и "<" тоже, поэтому я получил этот код для изменения структура био, чтобы использовать его. </p>
Вот мой код, чтобы изменить структуру био
import re
import json
import pprint
bio_regex = re.compile(
r"""
("bio":\s*{) # bio field start
(.*?) # content
(},) # bio field end
(?=\s*(?:"\w+"|})) # followed by another one or the json end
""",
flags=re.VERBOSE | re.DOTALL)
value_regex = re.compile(
r"""
("value":\s*") # value field start
(.*?) # content
("\s*\Z) # value field end + end of string
""",
flags=re.VERBOSE | re.DOTALL)
def normalize_value(mo):
start, content, end = mo.group(1, 2, 3)
content = content.replace('"', '\\"')
return start + content + end
def normalize_bio(mo):
start, content, end = mo.group(1, 2, 3)
content = value_regex.sub(normalize_value, content)
return start + content + end
messy_json = """
{
"bio":{
"type":"/type/text",
"value":"> "Eversley, William Pinder, B.C.L. Queen's Coll., Oxon, M.A., a member of the South-eastern circuit, reporter for Law Times in Queen's Bench division, a student of the Inner Temple 14 April, 1874 (then aged 23), called to the bar 25 April, 1877 (eldest son of William Eversley, Esq., of London); born u2060, 1851. rn> rn> 7, King's Bench Walk, Temple, E.C." rn> ...[in Foster's Men at the Bar][1]rnrnrn rnrn[1]: https://en.wikisource.org/wiki/Men-at-the-Bar/Eversley,_William_Pinder "Men at the Bar""
},
"name":"William Pinder Eversley",
"created":{
"type":"/type/datetime",
"value":"2008-04-01T03:28:50.625462"
},
"death_date":"1918",
"photos":[
6897255,
6897254
],
"last_modified":{
"type":"/type/datetime",
"value":"2018-07-31T15:39:07.982159"
},
"latest_revision":6,
"key":"/authors/OL1003081A",
"birth_date":"1851",
"personal_name":"William Pinder Eversley",
"type":{
"key":"/type/author"
},
"revision":6
}"""
result = bio_regex.sub(normalize_bio, messy_json)
obj = json.loads(result)
Вот результат:
{'bio': {'type': '/type/text',
'value': '> "Eversley, William Pinder, B.C.L. Queen\'s Coll., Oxon, M.A., a member of the '
"South-eastern circuit, reporter for Law Times in Queen's Bench division, a student of "
'the Inner Temple 14 April, 1874 (then aged 23), called to the bar 25 April, 1877 (eldest '
"son of William Eversley, Esq., of London); born u2060, 1851. rn> rn> 7, King's Bench "
'Walk, Temple, E.C." rn> ...[in Foster\'s Men at the Bar][1]rnrnrn rnrn[1]: '
'https://en.wikisource.org/wiki/Men-at-the-Bar/Eversley,_William_Pinder "Men at the Bar"'},
'birth_date': '1851',
'created': {'type': '/type/datetime', 'value': '2008-04-01T03:28:50.625462'},
'death_date': '1918',
'key': '/authors/OL1003081A',
'last_modified': {'type': '/type/datetime', 'value': '2018-07-31T15:39:07.982159'},
'latest_revision': 6,
'name': 'William Pinder Eversley',
'personal_name': 'William Pinder Eversley',
'photos': [6897255, 6897254],
'revision': 6,
'type': {'key': '/type/author'}}
Проблема здесь заключается в том, что этот сценарий хорош, если я помещаю всю свою строку в код, но я хотел бы восстановить 1000000 строк био с хорошей структурой, я не могу сделать это 1 на 1, я много чего пробовал с al oop чтобы восстановить 1 на 1, но это всегда приводит меня к ошибке, мне нужно знать, как вылечить его с oop Мне нужно обновить мой код, чтобы взять все строки базы данных из строки био, а не только 1 на 1
Заранее спасибо и слушайте меня!