UnicodeDecodeError:'utf8'编解码器无法解码位置2的字节0xdf

时间:2014-10-29 08:26:28

标签: python mongodb python-unicode

我写了一个源代码来推导股票的表现。它在一个实例中高效运行,但在我的本地运行时显示“UnicodeDecodeError”。我提到了这个python: UnicodeDecodeError: 'utf8' codec can't decode byte 0xc0 in position 0: invalid start byte,但我仍然对它在一个没有错误的实例中的工作方式感到困惑,我该如何解决呢?如果你能用解决方案回答我,我会非常感激,因为我是python中编码和解码的新手。

以下是发生错误的功能

def CollectFromDate(eb_grade, year, month):
    global monthly_days, end_date
    start_date = datetime.strptime('%s%s01' % (year, '0%s' % month if month<10 else month) , '%Y%m%d')
    No_days = monthrange(year, month)[1]
    end_date = datetime.strptime('%s%s%s' % (year,  '0%s' % month if month<10 else month, No_days) , '%Y%m%d')
    monthly_days = [(start_date+timedelta(i)).strftime('%Y%m%d') for i in range(No_days)]
    MemBackup = db.memcache_backup.find({'_id' : {'$in': monthly_days}})
    ScripFrom = {}
    for DATA in MemBackup:
        date = DATA.pop('_id')
        for scrip, val in DATA.items():
            valuation = val['pro_valuation_average']['result']
            if valuation in ['Strong Upside', 'Upside'] and val['eb_score'][1] in eb_grade and scrip not in ScripFrom:
                ScripFrom[scrip] = [{'from': date, 'from_val': valuation}]
    return ScripFrom

以下是错误

Traceback (most recent call last):
  File "/Users/faizalmohammed/Mvc-EB/lib/excellent_companies.py", line 201, in <module>
    pprint (ProcessPerformanceData(eb_grade, year, month))
  File "/Users/faizalmohammed/Mvc-EB/lib/excellent_companies.py", line 79, in ProcessPerformanceData
    ScripPerform = CollectFromDate(eb_grade, year, month)
  File "/Users/faizalmohammed/Mvc-EB/lib/excellent_companies.py", line 176, in CollectFromDate
    for DATA in MemBackup:
  File "/Library/Python/2.7/site-packages/pymongo/cursor.py", line 814, in next
    if len(self.__data) or self._refresh():
  File "/Library/Python/2.7/site-packages/pymongo/cursor.py", line 776, in _refresh
    limit, self.__id))
  File "/Library/Python/2.7/site-packages/pymongo/cursor.py", line 720, in __send_message
    self.__uuid_subtype)
  File "/Library/Python/2.7/site-packages/pymongo/helpers.py", line 112, in _unpack_response
    as_class, tz_aware, uuid_subtype)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xdf in position 2: invalid continuation byte

0 个答案:

没有答案