【问题标题】:UnicodeEncodeError when writing the selected file写入所选文件时出现 UnicodeEncodeError
【发布时间】:2017-06-29 03:51:40
【问题描述】:

我对已经回答过的其他问题进行了几次尝试,而且我的代码总是返回错误。 此代码的唯一目的是将标签放在文档的句子中,并将包含超过 N 次出现的特定 POS 的句子转储到文件中:

import os
import nlpnet
import codecs

TAGGER = nlpnet.POSTagger('pos-pt', language='pt')


# You could have a function that tagged and verified if a
# sentence meets the criteria for storage.

def is_worth_saving(text, pos, pos_count):
   # tagged sentences are lists of tagged words, which in
   # nlpnet are (word, pos) tuples. Tagged texts may contain
   # several sentences.
   pos_words = [word for sentence in TAGGER.tag(text)
             for word in sentence
             if word[1] == pos]
   return len(pos_words) >= pos_count



with codecs.open('dataset.txt', encoding='utf8') as original_file:
with codecs.open('dataset_new.txt', 'w') as output_file:
    for text in original_file:
        # For example, only save sentences with more than 5 verbs in it
        if is_worth_saving(text, 'V', 5):
            output_file.write(text + os.linesep)

编译错误:

Traceback (most recent call last):
   File "D:/Word Sorter/Classifier.py", line 31, in <module>
     output_file.write(text + os.linesep)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 161-162: ordinal not in range(128)

【问题讨论】:

    标签: python-2.7 text encoding ascii non-ascii-characters


    【解决方案1】:

    你以前见过这些问题吗?

    UnicodeEncodeError: 'ascii' codec can't encode character u'\xa0' in position 20: ordinal not in range(128)Again: UnicodeEncodeError: ascii codec can't encode

    这与您的错误完全相同。所以我的猜测是你需要使用text.encode('utf8') 对你的text 进行编码。

    编辑:

    在这里尝试使用它:

    output_file.write(text.encode('utf8') + os.linesep)
    

    【讨论】:

      猜你喜欢
      • 2011-10-19
      • 2016-12-21
      • 1970-01-01
      • 2014-09-26
      • 1970-01-01
      • 1970-01-01
      • 2014-03-05
      • 1970-01-01
      相关资源
      最近更新 更多