के साथ यूनिकोड समस्या मुझे पता है कि मुझे यूनिकोड से रूपांतरण में कोई समस्या है लेकिन मुझे यकीन नहीं है कि यह कहां हो रहा है।SQLAlchemy
मैं HTML फ़ाइलों की निर्देशिका से हाल ही में यूरोपेन यात्रा के बारे में डेटा निकाल रहा हूं। कुछ स्थान नामों में गैर-ASCII वर्ण हैं (जैसे é, ô, ü)। मुझे regex का उपयोग कर फ़ाइल के स्ट्रिंग प्रस्तुति से डेटा मिल रहा है।
तो मैं स्थानों प्रिंट के रूप में मैं उन्हें मिल जाए, वे पात्रों के साथ मुद्रित इतना एन्कोडिंग ठीक होना चाहिए:
Le Pré-Saint-Gervais, France
Hôtel-de-Ville, France
मैं का उपयोग कर एक SQLite तालिका में डेटा भंडारण कर रहा हूँ SQLAlchemy:
Base = declarative_base()
class Point(Base):
__tablename__ = 'points'
id = Column(Integer, primary_key=True)
pdate = Column(Date)
ptime = Column(Time)
location = Column(Unicode(32))
weather = Column(String(16))
high = Column(Float)
low = Column(Float)
lat = Column(String(16))
lon = Column(String(16))
image = Column(String(64))
caption = Column(String(64))
def __init__(self, filename, pdate, ptime, location, weather, high, low, lat, lon, image, caption):
self.filename = filename
self.pdate = pdate
self.ptime = ptime
self.location = location
self.weather = weather
self.high = high
self.low = low
self.lat = lat
self.lon = lon
self.image = image
self.caption = caption
def __repr__(self):
return "<Point('%s','%s','%s')>" % (self.filename, self.pdate, self.ptime)
engine = create_engine('sqlite:///:memory:', echo=False)
Base.metadata.create_all(engine)
Session = sessionmaker(bind = engine)
session = Session()
फ़ाइलों के माध्यम से मैं पाश और डेटाबेस में हर एक से डेटा सम्मिलित करें:
for filename in filelist:
# open the file and extract the information using regex such as:
location_re = re.compile("<h2>(.*)</h2>",re.M)
# extract other data
newpoint = Point(filename, pdate, ptime, location, weather, high, low, lat, lon, image, caption)
session.add(newpoint)
session.commit()
मैं प्रत्येक डालने पर निम्न चेतावनी देखें:
/usr/lib/python2.5/site-packages/SQLAlchemy-0.5.4p2-py2.5.egg/sqlalchemy/engine/default.py:230: SAWarning: Unicode type received non-unicode bind param value 'Spitalfields, United Kingdom'
param.append(processors[key](compiled_params[key]))
और जब मैं इस तरह के रूप तालिका के साथ कुछ भी करने की कोशिश:
Traceback (most recent call last):
File "./extract_trips.py", line 131, in <module>
session.query(Point).all()
File "/usr/lib/python2.5/site-packages/SQLAlchemy-0.5.4p2-py2.5.egg/sqlalchemy/orm/query.py", line 1193, in all
return list(self)
File "/usr/lib/python2.5/site-packages/SQLAlchemy-0.5.4p2-py2.5.egg/sqlalchemy/orm/query.py", line 1341, in instances
fetch = cursor.fetchall()
File "/usr/lib/python2.5/site-packages/SQLAlchemy-0.5.4p2-py2.5.egg/sqlalchemy/engine/base.py", line 1642, in fetchall
self.connection._handle_dbapi_exception(e, None, None, self.cursor, self.context)
File "/usr/lib/python2.5/site-packages/SQLAlchemy-0.5.4p2-py2.5.egg/sqlalchemy/engine/base.py", line 931, in _handle_dbapi_exception
raise exc.DBAPIError.instance(statement, parameters, e, connection_invalidated=is_disconnect)
sqlalchemy.exc.OperationalError: (OperationalError) Could not decode to UTF-8 column 'points_location' with text 'Le Pré-Saint-Gervais, France' None None
मैं चाहूँगा:
session.query(Point).all()
मैं सही ढंग से स्टोर करने में सक्षम होने के लिए और फिर मूल अक्षरों के साथ स्थान नामों को बरकरार रखें। कोई भी सहायताकाफी प्रशंसनीय होगी।
मैं "आईएसओ -885 9 -1" से पहले "सीपी 1252" पहले कोशिश करूंगा। और मुझे नहीं पता कि निम्नलिखित में क्या मदद करता है: http://stackoverflow.com/questions/368805/python-unicodedecodeerror-am-i-misunderstanding-encode/370199#370199 – tzot