小编kve*_*nda的帖子

如何让SQLAlchemy正确地将unicode省略号插入到mySQL表中?

我正在尝试使用feedparser解析RSS提要并使用SQLAlchemy将其插入到mySQL表中.我实际上能够让这个运行得很好但是今天feed在描述中有一个带省略号字符的项目,我得到以下错误:

UnicodeEncodeError:'latin-1'编解码器无法编码位置35中的字符u'\ u2026':序数不在范围内(256)

如果我将convert_unicode = True选项添加到引擎,我可以让插件通过,但省略号不会显示它只是奇怪的字符.这似乎是有道理的,因为据我所知,拉丁语1中没有水平省略号.即使我将编码设置为utf-8,它似乎没有什么区别.如果我使用phpmyadmin进行插入并包含省略号,它会很好.

我想我只是不理解字符编码或如何让SQLAlchemy使用我指定的字符编码.有没有人知道如何让文本进入没有奇怪的字符?

UPDATE

我想我已经想出了这个,但我不确定为什么这很重要......

这是代码:

import sys
import feedparser
import sqlalchemy
from sqlalchemy import create_engine, MetaData, Table

COMMON_CHANNEL_PROPERTIES = [
  ('Channel title:','title', None),
  ('Channel description:', 'description', 100),
  ('Channel URL:', 'link', None),
]

COMMON_ITEM_PROPERTIES = [
  ('Item title:', 'title', None),
  ('Item description:', 'description', 100),
  ('Item URL:', 'link', None),
]

INDENT = u' '*4

def feedinfo(url, output=sys.stdout):
  feed_data = feedparser.parse(url)
  channel, items = feed_data.feed, feed_data.entries

  #adding charset=utf8 here is what fixed the problem

  db = create_engine('mysql://user:pass@localhost/db?charset=utf8')
  metadata …
Run Code Online (Sandbox Code Playgroud)

python mysql unicode sqlalchemy feedparser

16
推荐指数
1
解决办法
1万
查看次数

标签 统计

feedparser ×1

mysql ×1

python ×1

sqlalchemy ×1

unicode ×1