inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

프로그래밍, 데이터 과학을 위한 파이썬 입문

Lab: JSON Data Analysis 1

XML Read -> JSON write 숙제 제출

135

레오

작성한 질문수 4

0

낙서장 같은 코드..
이번 강의도 감사합니다!
질문. BeautifulSoup 모듈을 사용할 때는 항상 아래처럼 bs4와 함께 import해야 하나요?
from bs4 import BeautifulSoup

import os
import re
import json
from bs4 import BeautifulSoup

# SEPARATE XML=================================================================
# setting
raw_xml_file = "ipa110106.XML"
target_root = "data"

# read raw xml
with open(raw_xml_file, "r") as raw_xml:
    xml_contents = raw_xml.read()

# separate xml contents
chopped_txts = re.findall(r'(<\?xml)([\s\S]+?)(</us-patent-application>)', xml_contents)
chopped_txts = [''.join(tuples) for tuples in chopped_txts]

# check dir
if not os.path.isdir(target_root):
    os.mkdir(target_root)

# make new files with separated xml contents
for txt in chopped_txts:
    new_file_name = re.findall(r'(<us-patent-application.+file=")(.+XML)', txt)
    with open(os.path.join(target_root, new_file_name[0][1]), "w") as new_xml:
        new_xml.write(txt)

# MAKE DICTIONARY==============================================================
# setting
data_dict = dict()
source_root = "data"
source_xml_files = os.listdir(source_root)

# make Dictionary Data from Xml
for xml_file in source_xml_files:

    with open(os.path.join(source_root, xml_file), "r") as xml_file:
        # xml tags
        soup = BeautifulSoup(xml_file, "lxml")
        publication_reference_tag = soup.find("publication-reference")
        application_reference_tag = soup.find("application-reference")
        p_document_id_tag = publication_reference_tag.find("document-id")
        a_document_id_tag = application_reference_tag.find("document-id")

        patent_dict = dict()  # reset

        # extract info from Xml
        p_country = p_document_id_tag.find("country").get_text()  # 등록국가
        p_doc_number = p_document_id_tag.find("doc-number").get_text()  # 등록번호
        p_kind = p_document_id_tag.find("kind").get_text()  # 등록상태
        p_date = p_document_id_tag.find("date").get_text()  # 등록일자
        a_country = a_document_id_tag.find("country").get_text()  # 출원국가
        a_doc_number = a_document_id_tag.find("doc-number").get_text()  # 출원번호
        a_date = a_document_id_tag.find("date").get_text()  # 출원일
        invention_title = soup.find("invention-title").get_text()  # 특허제목

        # store info in Patent Dict
        patent_dict["publication-country"] = p_country
        patent_dict["publication-number"] = p_doc_number
        patent_dict["publication-kind"] = p_kind
        patent_dict["publication-date"] = p_date
        patent_dict["application-country"] = a_country
        patent_dict["application-number"] = a_doc_number
        patent_dict["application-date"] = a_date
        patent_dict["invention-title"] = invention_title

        # add patent info in data dict
        # key value is publication-doc-number
        data_dict[p_doc_number] = patent_dict

# EXPORT TO JSON===============================================================
# setting
output_root = "output"
output_file = "my_first_json.json"

# check dir
if not os.path.isdir(output_root):
    os.mkdir(output_root)
# (over)write on json file
with open(os.path.join(output_root, output_file), "w") as json_file:
    json.dump(data_dict, json_file)

python bigdata

답변 0

atom warning 글

0

337

0

backend.ai 설치 오류

0

315

0

backend.ai 설치 시 에러 발생

0

740

1

과제 제출 시 hash key is already used 문제 발생

0

445

0

install.bat 오류

0

478

0

과제 제출 시 hash key is already used. 해시키 문제 문의 드립니다.

0

308

1

"Hash key is already used."으로 인한 과제 제출 실패 문의

0

303

1

과제 제출 시 에러가 발생합니다

0

271

1

인트로 강의가 없습니다

0

180

0

사진 링크가 깨져있습니다

0

270

0

slack아직 사용할 수 있는건가요?

0

173

0

강의자료

0

398

4

'backend.ai'은(는) 내부 또는 외부 명령, 실행할 수 있는 프로그램, 또는 배치 파일이 아닙니다.

0

438

2

에러확인부탁드립니다.

0

258

1

number of cases 코드 질문

0

206

1

split 관련 질문입니다!

0

229

1

함수 definition 관련 질문입니다.

0

296

1

is_digit 함수 구현

0

306

1

Map & Reduce 강의 8분 15초

1

233

1

keyword parameter

0

239

1

submit.bat 오류

0

196

2

matrix_transpose 결과값이 뭐죠?

0

176

0

value를 넣으면 key를 반환하는 함수

0

131

0

첨부파일의 unit test는 어떻게 사용하나요?

0

267

0