自分で作成したプログラムに一般的なファイルを読み込むのに問題があります。私が現在抱えている問題は、PDFが、私の操作全体にレンチを投げ込むBOMを含むある種の変異したutf-8に基づいていることです。私のアプリケーションでは、ASCII入力を必要とするSnowballステミングアルゴリズムを使用しています。utf-8に向けてエラーを解決することに関するトピックは多数ありますが、Snowballアルゴリズムにエラーを送信することや、ASCIIが最終結果になりたいという事実を考慮することはありません。現在使用しているファイルは、標準のANSIエンコーディングを使用したメモ帳ファイルです。私が受け取る特定のエラーメッセージはこれです:
File "C:\Users\svictoroff\Desktop\Alleyoop\Python_Scripts\Keywords.py", line 38, in Map_Sentence_To_Keywords
Word = Word.encode('ascii', 'ignore')
UnicodeDecodeError: 'ascii' codec can't decode byte 0x96 in position 0: ordinal not in range(128)
私の理解では、Python内では、ignore引数を含めると、検出されたASCII以外の文字が渡されるだけであり、このようにしてBOMや特殊文字をバイパスしますが、明らかにそうではありません。呼び出される実際のコードは次のとおりです。
def Map_Sentence_To_Keywords(Sentence, Keywords):
'''Takes in a sentence and a list of Keywords, returns a tuple where the
first element is the sentence, and the second element is a set of
all keywords appearing in the sentence. Uses Snowball algorithm'''
Equivalence = stem.SnowballStemmer('english')
Found = []
Sentence = re.sub(r'^(\W*?)(.*)(\n?)$', r'\2', Sentence)
Words = Sentence.split()
for Word in Words:
Word = Word.lower().strip()
Word = Word.encode('ascii', 'ignore')
Word = Equivalence.stem(Word)
Found.append(Word)
return (Sentence, Found)
文字列の前に一般的な貪欲でない非文字の正規表現の削除を含めることで、問題のある文字が削除されることも期待していましたが、これもそうではありません。私はASCII以外の多くのエンコーディングを試みましたが、厳密なbase64エンコーディングは機能しますが、私のアプリケーションには非常に理想的ではありません。自動化された方法でこれを修正する方法についてのアイデアはありますか?
Elementの初期デコードは失敗しますが、実際にエンコーダーに渡されると、Unicodeエラーを返します。
for Element in Curriculum_Elements:
try:
Element = Element.decode('utf-8-sig')
except:
print Element
Curriculum_Tuples.append(Map_Sentence_To_Keywords(Element, Keywords))
def scraping(File):
'''Takes in txt file of curriculum, removes all newlines and returns that occur \
after a lowercase character, then splits at all remaining newlines'''
Curriculum_Elements = []
Document = open(File, 'rb').read()
Document = re.sub(r'(?<=[a-zA-Z,])\r?\n', ' ', Document)
Curriculum_Elements = Document.split('\r\n')
return Curriculum_Elements
示されているコードは、見られているカリキュラム要素を生成します。
for Element in Curriculum_Elements:
try:
Element = unicode(Element, 'utf-8-sig', 'ignore')
except:
print Element
この型キャストのハックアラウンドは実際には機能しますが、ASCIIへの変換は少し不安定です。このエラーを返します:
Warning (from warnings module):
File "C:\Python27\lib\encodings\utf_8_sig.py", line 19
if input[:3] == codecs.BOM_UTF8:
UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal