python mysql UnicodeEncodeError'latin-1'编解码器无法编码字符'\ u2019'

时间:2017-01-22 19:08:44

标签: python

编写一个python程序将数据插入到mysql表中,当运行以下代码时,它会出错:

def public_figure_db_manage(self,type, posts, public_figure_id, public_figure_name):
...
    db_data = posts
    if type == "public_figure_posts":
        for item in db_data:
            is_exist_id_sql = "select * from fb_public_figure_posts where Post_ID = '" + item['id'] + "'"
            if cur.execute(is_exist_id_sql) == 0:
                insert_sql = "INSERT INTO fb_public_figure_posts VALUES ('{0}','{1}','{2}','{3}','{4}',{5},'{6}','{7}',{8},{9},{10})".format(item['id'],item['permalink_url'],item['created_time'],item['updated_time'],item['from']['name'],item['from']['id'],item['message'],item['link'],item['likes']['summary']['total_count'],item['comments']['summary']['total_count'],public_figure_id)
                cur.execute(insert_sql)

错误:

Traceback (most recent call last):
  File "C:/Python/PyCharmProject/FaceBookCrawl/FBCrawl.py", line 268, in <module>
    publicfigure_download.public_figure_posts_storage(public_figure_name)
  File "C:\Python\PyCharmProject\FaceBookCrawl\publicfigure_download.py", line 103, in public_figure_posts_storage
    mysql_manage().public_figure_db_manage(type, posts, public_figure_id, public_figure_name)
  File "C:\Python\PyCharmProject\FaceBookCrawl\database_manage.py", line 42, in public_figure_db_manage
    cur.execute(insert_sql)
  File "C:\Python\Python36\lib\site-packages\pymysql\cursors.py", line 166, in execute
    result = self._query(query)
  File "C:\Python\Python36\lib\site-packages\pymysql\cursors.py", line 322, in _query
conn.query(q)
  File "C:\Python\Python36\lib\site-packages\pymysql\connections.py", line 833, in query
    sql = sql.encode(self.encoding, 'surrogateescape')
UnicodeEncodeError: 'latin-1' codec can't encode character '\u2019' in position 242: ordinal not in range(256)


Process finished with exit code 1

指出句子错误:

cur.execute(insert_sql)

这似乎是代码问题,请你帮帮我

0 个答案:

没有答案