UnicodeEncodeError:'ascii'编解码器无法编码位置26-27中的字符:序数不在范围内(128)

时间:2016-10-19 00:10:08

标签: python facebook heroku encoding

我正在运行一个运行python脚本的作业,从Facebook Graph API收集Facebook事件数据,并使用Heroku Scheduler打印出有关事件的信息。由于它是一个在云中的特定时间每天运行的自动化作业,因此当Heroku Scheduler在云中运行该作业时,将打印到屏幕上的Heroku日志。

奇怪的是,当我通过执行heroku run命令手动运行python脚本时没有问题,但是当它由Heroku Scheduler在云中自动运行并记录打印时,会出现编码错误。

带错误的相关python脚本如下:

def importFromPages():
    sources = sources_collection.find(
        projection= {
            'id'        : 1,
            'source'    : 1,
            'name'      : 1,
            '_id'       : 0
        }
    )

    headers = {
        'Content-Type'  : 'application/json'
    }

    params = {
        'fields'        : 'id, name, cover, start_time, end_time, place',
        'access_token'  : FB_ACCESS_TOKEN
    }

    for source in sources:
        print(source['name'])
        data = requests.get(
            'https://graph.facebook.com/v2.7/{}/events'.format(source['id']),
            params=params,
            headers=headers
        )

        if data.status_code == 200:
            data = json.loads(data.text)
            data = data['data']
            if len(data) == 0: continue

            for event in data:
                now     = arrow.now().timestamp
                end     = arrow.get(event.get('end_time', None)).timestamp

                if end != None:
                    if end <= now: continue

                print(event['id'])
                event['host'] = source
                events_collection.update(
                    { 'id'      : event['id'] },
                    { '$set'    : event },
                    upsert=True
                )
        else:
            print('data.status_code != 200')

在计划运行作业时显示编码错误的heroku日志位于:

2016-10-18T12:31:34.270891+00:00 app[scheduler.1368]: UNSW Bitfwd
2016-10-18T12:31:34.411757+00:00 app[scheduler.1368]: IGA UNSW
2016-10-18T12:31:34.531127+00:00 app[scheduler.1368]: UNSW Barbell Club
2016-10-18T12:31:34.721310+00:00 app[scheduler.1368]: 660525034113560
2016-10-18T12:31:34.724900+00:00 app[scheduler.1368]: Circusoc - The UNSW Circus Society
2016-10-18T12:31:34.947553+00:00 app[scheduler.1368]: UNSW Medical Music Society
2016-10-18T12:31:35.140233+00:00 app[scheduler.1368]: 1166082866820866
2016-10-18T12:31:35.145683+00:00 app[scheduler.1368]: MODsoc Ministry of Dance UNSW
2016-10-18T12:31:35.370460+00:00 app[scheduler.1368]: Arc at UNSW Art & Design
2016-10-18T12:31:35.616661+00:00 app[scheduler.1368]: 1145482732204348
2016-10-18T12:31:35.622455+00:00 app[scheduler.1368]: Accounting Society Of UNSW (AccSoc)
2016-10-18T12:31:35.815662+00:00 app[scheduler.1368]: 1375370362476567
2016-10-18T12:31:35.817958+00:00 app[scheduler.1368]: 1679765869019301
2016-10-18T12:31:35.821599+00:00 app[scheduler.1368]: Traceback (most recent call last):
2016-10-18T12:31:35.821601+00:00 app[scheduler.1368]:   File "data.py", line 127, in <module>
2016-10-18T12:31:35.821652+00:00 app[scheduler.1368]:     main()
2016-10-18T12:31:35.821655+00:00 app[scheduler.1368]:   File "data.py", line 124, in main
2016-10-18T12:31:35.821709+00:00 app[scheduler.1368]:     importFromPages()
2016-10-18T12:31:35.821711+00:00 app[scheduler.1368]:   File "data.py", line 92, in importFromPages
2016-10-18T12:31:35.821715+00:00 app[scheduler.1368]:     print(source['name'])
2016-10-18T12:31:35.821745+00:00 app[scheduler.1368]: UnicodeEncodeError: 'ascii' codec can't encode characters in position 26-27: ordinal not in range(128)
2016-10-18T12:31:36.333406+00:00 heroku[scheduler.1368]: Process exited with status 1
2016-10-18T12:31:36.337350+00:00 heroku[scheduler.1368]: State changed from up to complete

当我使用heroku运行手动运行作业时,如下所示,没有编码打印错误,并且作业能够完成而没有任何错误:

heroku run python data.py
Running python data.py on ⬢ eventobotdatacollector... up, run.4056 (Free)

有没有人知道这里的问题是什么以及为什么不同的环境会导致错误?

1 个答案:

答案 0 :(得分:3)

我不知道不同环境的影响。但我能够通过明确unicode编码我想要打印出来的数据(或者写入文件)来解决类似的问题。尝试

print(source['name'].encode('utf-8'))

或者在这里查看,以获得有关Python中unicode问题的更完整和知识渊博的解释:SQLite, python, unicode, and non-utf data

希望这会有所帮助。的问候,