프로그래밍 처음 소개. 필자는 디렉토리에서 XML 파일을 읽고 요소의 텍스트를 복사하여 출력 디렉토리에 써 넣기 위해 BeautifulSoup를 사용하는 Python 코드 (stackoverflow의 도움을 받아)를 작성했습니다.Python : 여러 파일을 한 번에 쓸 수있는 스크립트 실행
from bs4 import BeautifulSoup
import os, sys
source_folder='/Python27/source_xml'
for article in os.listdir(source_folder):
soup=BeautifulSoup(open(source_folder+'/'+article))
#I think this strips any tags that are nested in the sample_tag
clean_text=soup.sample_tag.get_text(" ",strip=True)
#This grabs an ID which I used as the output file name
article_id=soup.article_id.get_text(" ",strip=True)
with open(article_id,"wb") as f:
f.write(clean_text.encode("UTF-8"))
문제는 내가 10 만 개 XML 파일을 실행하려면이 필요하다 : 여기 내 코드입니다. 나는 지난 밤에 프로그램을 시작했고, 나의 견적에 달리는 데 15 시간이 걸렸습니다. 한 번에 하나씩 읽고 쓰는 대신 한 번에 100 또는 1000을 읽거나 쓸 수있는 방법이 있습니까?
파일 작성이 가장 느린 단계인지 확인 했습니까? BeautifulSoup로 XML 파일을 파싱하지 않습니까? –
구문 분석은 병목 현상이 아니라고 생각합니다. 각 XMl 파일의 평균 크기는 약 50KB이기 때문입니다. – Levar