полоса html, встроенный стиль и новая строка, символы табуляции от json в python - PullRequest
0 голосов
/ 07 февраля 2019

У меня есть внешний файл json, в котором есть встроенное кодирование, html-теги, \ n и \ t-символы. Я хочу удалить все эти вещи и сохранить только строки без разрыва формата json. До сих пор я пробовал это и виделмного решений, но ничего не сработало.Действительно ценю за ваше время.Вот мой код

Я использую Python 3.xx

import json, re
from html.parser import HTMLParser

def remove_html_tags(data):
    p = re.compile(r'<.*?>')
    return p.sub('', data)

with open('project-closedtasks-avgdaysopen.json') as f:
    data = json.load(f)
    data = json.dumps(data, indent=4)
print(data)

Обратите внимание, это файл, который я получаю (импорт из той же папки) И я хочу такой же вывод, но нетhtml-теги, нет встроенного стиля, нет \ n или других вещей, только строка.

[
    {
        "idrfi" : 36809,
        "fkproject" : 33235,
        "subject" : "M2 - Flashing Clarifications",
        "description" : "<ol style=\"margin-left:0.375in\">\n\t<li><span style=\"font-family:calibri; font-size:11pt\">Refer to detail 5/A650 attached. Can the pre-finished metal panel be swapped for pre-finished metal flashing? This will allow the full assembly to be installed by the mechanical HVAC trade vs requiring the cladding trade to return for penthouse work. </span></li>\n</ol>\n",
        "response" : null
    },
    {
        "idrfi" : 36808,
        "fkproject" : 33139,
        "subject" : "M1 - Flashing Clarifications",
        "description" : "<ol style=\"margin-left:0.2in\">\n\t<li><span style=\"font-family:calibri; font-size:11pt\">Refer to detail 6/A612 attached. Clarify location of flashing on detail.</span></li>\n\t<li><span style=\"font-family:calibri; font-size:11pt\">Refer to details 2,4/A614 attached. Clarify location of flashing on detail. </span></li>\n\t<li><span style=\"font-family:calibri; font-size:11pt\">Refer to detail 3/A616 attached. Clarify location of flashing on detail.</span></li>\n\t<li><span style=\"font-family:calibri; font-size:11pt\">Refer to detail 5/A650 attached. Can the pre-finished metal panel be swapped for pre-finished metal flashing? This will allow the full assembly to be installed by the mechanical HVAC trade vs requiring the cladding trade to return for penthouse work. </span></li>\n</ol>\n",
        "response" : null
    }
]

Я нашел функцию, но не знаю, как ее реализовать

def remove_html_tags(data):
    p = re.compile(r'<.*?>')
    return p.sub('', data)

отредактировано после этой реализации, \ n, \ t и другие вещи не удаляются. Я хочу только строку без тегов без стиля ничего

import json, re
from html.parser import HTMLParser

def remove_html_tags(data):
    p = re.compile(r'<.*?>')
    return p.sub('', data)

with open('project-closedtasks-avgdaysopen.json') as f:
    data = json.load(f)
    data = json.dumps(data, indent=4)
    removed_tags = remove_html_tags(data)
print(removed_tags)

1 Ответ

0 голосов
/ 07 февраля 2019

Просто вызовите ту функцию, которую вы написали

import json, re
from html.parser import HTMLParser

def remove_html_tags(data):
    p = re.compile(r'<.*?>')
    return p.sub('', data)

with open('project-closedtasks-avgdaysopen.json') as f:
    data = json.load(f)
    data = json.dumps(data, indent=4)
    removed_tags = remove_html_tags(data)
print(removed_tags)

Я проверил, она работает нормально.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...