inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

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

Lab: XML Parsing

결과물을 보면 행이 바뀔때 \n이 한개가 있는게 아니라 \n\n 적용이 됩니다.

154

김동혁

작성한 질문수 48

0

from bs4 import BeautifulSoup

import csv

with open("ipa110106.XML", "r", encoding="utf8") as source_file:

xml = source_file.read()

text = '<?xml version="1.0" encoding="UTF-8"?>'

xml_list = xml.split(text)

line_header = 0

header_list = ["publication_doc_number", "publication_date", "application_doc_number", "application_date", "invention_title"]

with open("data.csv", "w", encoding="utf8") as destination_file:

for temp in xml_list:

if len(temp) == 0:

continue

else:

if line_header == 0:

destination_file.write(",".join(header_list)+"n")

line_header += 1

else:

line_header += 1

soup = BeautifulSoup(temp, "lxml")

publication_reference = soup.find("publication-reference")

publication_doc_number = publication_reference.find("doc-number")

publication_date = publication_reference.find("date")

application_reference = soup.find("application-reference")

application_doc_number = application_reference.find("doc-number")

application_date = application_reference.find("date")

invention_title = soup.find("invention-title")

writer = csv.writer(destination_file, delimiter=',', quoting=csv.QUOTE_MINIMAL)

writer.writerow([publication_doc_number.get_text(), publication_date.get_text(), application_doc_number.get_text(), application_date.get_text(), invention_title.get_text()])

output

publication_doc_number publication_date application_doc_number application_date invention_title

20110000003 20110106 12460569 20090721 Wetsuit made with a non-absorbent and quick drying fabric

20110000004 20110106 12459737 20090706 Fingerezz

20110000005 20110106 12498332 20090706 POSTURE IMPROVING GARMENT

20110000006 20110106 12497914 20090706 HEEL PROTECTORS

한줄씩 더 띄어지는데 문제가 뭘까요 교수님..?ㅠ

bigdata python

답변 1

0

TeamLab

아마 \r\n 이 들어가서 그런듯 합니다. lineterminator를 아래처럼 설정해줄 수 있습니다.

csv.register_dialect('myDialect', delimiter = '|', lineterminator = 'rnrn')

with open('lineterminator.csv', 'w') as f:

writer = csv.writer(f, dialect='myDialect')

writer.writerows(csvData)

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