2016-07-11 2 views
0

이 코드를 사용하여 로컬로 저장된 HTML 파일의 일부를 추출하고 축약 된 새 문서를 .txt 파일로 저장합니다.RegEX로 일반 HTML 파일 자르기

Traceback (most recent call last): 
    File "C:/Users/6930p/PycharmProjects/untitled/Versuch/CutFile.py", line 16, in <module> 
    extractor() 
    File "C:/Users/6930p/PycharmProjects/untitled/Versuch/CutFile.py", line 14, in extractor 
    out.write(cut) 
    File "C:\Users\6930p\Anaconda3\lib\encodings\cp1252.py", line 19, in encode 
    return codecs.charmap_encode(input,self.errors,encoding_table)[0] 
UnicodeEncodeError: 'charmap' codec can't encode characters in position 241205-241210: character maps to <undefined> 

누구나 아이디어 문제가 무엇 :

import glob 
import os 
import re 


def extractor(): 
    os.chdir(r"F:\Test") # the directory containing your html 
    for file in glob.iglob("*.html"): # iterates over all files in the directory ending in .html 
     with open(file, encoding="utf8") as f, open((file.rsplit(".", 1)[0]) + ".txt", "w", encoding="utf8") as out: 
      contents = f.read() 
      extract = re.compile(r'(Start).*?End', re.I | re.S) 
      cut = extract.sub('', contents) 
      if re.search(extract, contents) is not None: 
       out.write(cut) 
      out.close() 
extractor() 

내가 어떤 인코딩 문제가 얻을 할 몇 가지 파일 그러나 내 파일의 대부분 잘 작동? 사용하여 생각했습니다 encoding="utf8" 인코딩에 문제가 없습니다 ...

도움을 주셨습니다.

답변

0

좋아, 그게 encoding="utf8"와 관련된 문제입니다. 새롭게 생성 된 .txt 파일을 "utf8"으로 인코딩하는 것을 잊었습니다. 코드가 업데이트되어 작동합니다!