यूनिकोडडॉफॉर्सेट: 'एससीआई' कोडक 0xe2 को 13 की स्थिति में बाइट को डिकोड नहीं कर सकता: क्रम में नहीं (128)


129

मैं एनएलटीके का उपयोग कर रहा हूं, अपनी पाठ फ़ाइल पर किमीन क्लस्टरिंग कर रहा हूं जिसमें प्रत्येक पंक्ति को एक दस्तावेज माना जाता है। उदाहरण के लिए, मेरी पाठ फ़ाइल कुछ इस प्रकार है:

belong finger death punch <br>
hasty <br>
mike hasty walls jericho <br>
jägermeister rules <br>
rules bands follow performing jägermeister stage <br>
approach 

अब मैं जो डेमो कोड चलाने की कोशिश कर रहा हूं वह यह है:

import sys

import numpy
from nltk.cluster import KMeansClusterer, GAAClusterer, euclidean_distance
import nltk.corpus
from nltk import decorators
import nltk.stem

stemmer_func = nltk.stem.EnglishStemmer().stem
stopwords = set(nltk.corpus.stopwords.words('english'))

@decorators.memoize
def normalize_word(word):
    return stemmer_func(word.lower())

def get_words(titles):
    words = set()
    for title in job_titles:
        for word in title.split():
            words.add(normalize_word(word))
    return list(words)

@decorators.memoize
def vectorspaced(title):
    title_components = [normalize_word(word) for word in title.split()]
    return numpy.array([
        word in title_components and not word in stopwords
        for word in words], numpy.short)

if __name__ == '__main__':

    filename = 'example.txt'
    if len(sys.argv) == 2:
        filename = sys.argv[1]

    with open(filename) as title_file:

        job_titles = [line.strip() for line in title_file.readlines()]

        words = get_words(job_titles)

        # cluster = KMeansClusterer(5, euclidean_distance)
        cluster = GAAClusterer(5)
        cluster.cluster([vectorspaced(title) for title in job_titles if title])

        # NOTE: This is inefficient, cluster.classify should really just be
        # called when you are classifying previously unseen examples!
        classified_examples = [
                cluster.classify(vectorspaced(title)) for title in job_titles
            ]

        for cluster_id, title in sorted(zip(classified_examples, job_titles)):
            print cluster_id, title

(जो यहां भी पाया जा सकता है )

मुझे प्राप्त होने वाली त्रुटि यह है:

Traceback (most recent call last):
File "cluster_example.py", line 40, in
words = get_words(job_titles)
File "cluster_example.py", line 20, in get_words
words.add(normalize_word(word))
File "", line 1, in
File "/usr/local/lib/python2.7/dist-packages/nltk/decorators.py", line 183, in memoize
result = func(*args)
File "cluster_example.py", line 14, in normalize_word
return stemmer_func(word.lower())
File "/usr/local/lib/python2.7/dist-packages/nltk/stem/snowball.py", line 694, in stem
word = (word.replace(u"\u2019", u"\x27")
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 13: ordinal not in range(128)

यहाँ क्या हो रहा है?

जवाबों:


133

फ़ाइल को strs के एक समूह के रूप में पढ़ा जा रहा है , लेकिन इसे unicodes होना चाहिए । अजगर को बदलने की कोशिश करता है, लेकिन विफल रहता है। परिवर्तन:

job_titles = [line.strip() for line in title_file.readlines()]

स्पष्ट रूप से करने के लिए डिकोड strकरने के लिए रों unicode(यहाँ UTF-8 कल्पना करते हुए):

job_titles = [line.decode('utf-8').strip() for line in title_file.readlines()]

यह भी आयात करके हल किया जा सकता मॉड्यूल और का उपयोग कर से निर्मित नहीं बल्कि ।codecscodecs.openopen


2
इस लाइन को चलाने। timecode ('utf-8')। पट्टी ()। निचला ()। विभाजन () भी मुझे एक ही त्रुटि देता है। मैंने .deocode ('utf-8') को जोड़ा है
अमन माथुर

@ कथिर्रजा: क्या आप उसके लिए एक संदर्भ प्रदान कर सकते हैं? जहां तक ​​मुझे पता है, पायथन 3 में भी, decodeविधि यूनिकोड स्ट्रिंग को बाइट स्ट्रिंग को डिकोड करने का पसंदीदा तरीका है। (हालांकि, मेरा उत्तर में प्रकार अजगर 3 के लिए सही नहीं हैं - अजगर 3 के लिए, हम से परिवर्तित करने के लिए कोशिश कर रहे हैं bytesकरने के लिए strसे के बजाय strकरने के लिए unicode।)
icktoofay

52

यह मेरे लिए ठीक काम करता है।

f = open(file_path, 'r+', encoding="utf-8")

एन्कोडिंग प्रकार 'utf-8' सुनिश्चित करने के लिए आप एक तीसरा पैरामीटर एन्कोडिंग जोड़ सकते हैं

नोट: यह तरीका Python3 में ठीक काम करता है, मैंने इसे Python2.7 में आजमाया नहीं।


यह पायथन 2.7.10 में काम नहीं करता है:TypeError: 'encoding' is an invalid keyword argument for this function
बोरहान काज़िमिपोर

2
यह Python 2.7.10 में काम नहीं करता है: TypeError: 'encoding' is an invalid keyword argument for this function यह ठीक काम करता है:import io with io.open(file_path, 'r', encoding="utf-8") as f: for line in f: do_something(line)
Borhan Kazimipour

2
Python3.6 में एक आकर्षण की तरह काम किया बहुत बहुत धन्यवाद!
एसआरसी

32

मेरे लिए टर्मिनल एन्कोडिंग के साथ एक समस्या थी। UTF-8 को .bashrc में जोड़ने से समस्या हल हो गई:

export LC_CTYPE=en_US.UTF-8

बाद में पुनः लोड करना न भूलें।

source ~/.bashrc

3
मुझे export LC_ALL=C.UTF-8उबंटू 18.04.3 और पायथन 3.6.8 पर उपयोग करना था । अन्यथा इससे मेरी समस्या हल हो गई, धन्यवाद।
जुबांस्की

31

आप यह भी कोशिश कर सकते हैं:

import sys
reload(sys)
sys.setdefaultencoding('utf8')

3
इस के आशय क्या हैं? ऐसा लगता है कि यह कुछ वैश्विक है और न केवल इस फ़ाइल के लिए लागू है।
सिमेग

2
सूचना है कि इसके बाद के संस्करण अजगर 3. में पदावनत किया गया है
gented

12

जब Ubuntu 18.04 पर Python3.6 का उपयोग करके मैंने दोनों को करने में समस्या हल की है:

with open(filename, encoding="utf-8") as lines:

और अगर आप टूल को कमांड लाइन के रूप में चला रहे हैं:

export LC_ALL=C.UTF-8

ध्यान दें कि यदि आप Python2.7 में हैं तो आपको इसे अलग तरीके से संभालना होगा। पहले आपको डिफ़ॉल्ट एन्कोडिंग सेट करना होगा:

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

और फिर उस फाइल को लोड करने के लिए जिसे आपको io.openएन्कोडिंग सेट करने के लिए उपयोग करना होगा :

import io
with io.open(filename, 'r', encoding='utf-8') as lines:

आपको अभी भी env को निर्यात करने की आवश्यकता है

export LC_ALL=C.UTF-8

6

मुझे यह त्रुटि तब मिली जब एक डॉकटर कंटेनर में अजगर पैकेज स्थापित करने का प्रयास किया गया। मेरे लिए, मुद्दा यह था कि डॉकटर छवि को localeकॉन्फ़िगर नहीं किया गया था । Dockerfile में निम्न कोड जोड़ने से मेरे लिए समस्या हल हो गई।

# Avoid ascii errors when reading files in Python
RUN apt-get install -y \
  locales && \
  locale-gen en_US.UTF-8
ENV LANG='en_US.UTF-8' LANGUAGE='en_US:en' LC_ALL='en_US.UTF-8'

मुझे इसका उपयोग करना था: github.com/docker-library/python/issues/13
mayrop

3

किसी भी और सभी यूनिकोड त्रुटि से संबंधित खोजने के लिए ... निम्नलिखित कमांड का उपयोग करना:

grep -r -P '[^\x00-\x7f]' /etc/apache2 /etc/letsencrypt /etc/nginx

में मिला

/etc/letsencrypt/options-ssl-nginx.conf:        # The following CSP directives don't use default-src as 

का उपयोग करते हुए shed, मैं अपमानजनक अनुक्रम पाया। यह एक संपादक की गलती थी।

00008099:     C2  194 302 11000010
00008100:     A0  160 240 10100000
00008101:  d  64  100 144 01100100
00008102:  e  65  101 145 01100101
00008103:  f  66  102 146 01100110
00008104:  a  61  097 141 01100001
00008105:  u  75  117 165 01110101
00008106:  l  6C  108 154 01101100
00008107:  t  74  116 164 01110100
00008108:  -  2D  045 055 00101101
00008109:  s  73  115 163 01110011
00008110:  r  72  114 162 01110010
00008111:  c  63  099 143 01100011
00008112:     C2  194 302 11000010
00008113:     A0  160 240 10100000


0

अजगर 3 के लिए, डिफ़ॉल्ट एन्कोडिंग "utf-8" होगा। आधार दस्तावेज़ में निम्नलिखित चरणों का सुझाव दिया गया है: https://docs.python.org/2/library/csv.html#csv-examples किसी भी समस्या के मामले में

  1. एक फंक्शन बनाएं

    def utf_8_encoder(unicode_csv_data):
        for line in unicode_csv_data:
            yield line.encode('utf-8')
  2. फिर उदाहरण के लिए, पाठक के अंदर फ़ंक्शन का उपयोग करें

    csv_reader = csv.reader(utf_8_encoder(unicode_csv_data))

0

python3x या उच्चतर

  1. फ़ाइल को बाइट स्ट्रीम में लोड करें:

    शरीर = '' खुले में लाइनों के लिए ('वेबसाइट / index.html', 'आरबी'): डिकोडेडलाइन = लाइन्स।

  2. वैश्विक सेटिंग का उपयोग करें:

    आयात io आयात sys sys.stdout = io.TextIOWrapper (sys.stdout.buffer, एन्कोडिंग = 'utf-8')


हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.