You are not logged in.

#1 2008-10-05 21:57:56

Vintendo
Member
From: Netherlands
Registered: 2008-04-21
Posts: 375
Website

Python question

I am trying to write my own rss-reader using feedparser and curses. It's very simple and i use it to learn python, because i know the java stuff i learn at school already:lol:.

I get this error:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in position 47: ordinal not in range(128)

I fount out it gives this error beacuse it tries to combine unicode with ascii but i just cant figure it out.

The code

#!/usr/bin/env python
import getfeed
import curses

#curses startup
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
stdscr.keypad(1)

begin_x = 5 ; begin_y = 5
height = 40 ; width = 40
win = curses.newwin(height, width, begin_y, begin_x)

pad = curses.newpad(100, 100)

#tell getfeed to get and parse the feed  
feed = getfeed.getfeed("http://feeds.feedburner.com/tweakers/mixed")


for id in range(4):
    stdscr.addstr(1, 0, feed.getMessage(id))
    stdscr.refresh()
    id += 1

#Read ui and process it.
while 1:
    input = stdscr.getch()
    if input <= 51 & input >= 48:
        item = input - 48
        pad.addstr(0, 0, feed.getTitle(item))
        pad.addstr(1, 0, feed.getURL(item))
        pad.addstr(2, 0, feed.getMessage(item))
        pad.refresh(0,0, 1,1, 100,100)
        stdscr.refresh()
    if input == ord('x'):
        curses.nocbreak(); stdscr.keypad(0); curses.echo()
        curses.endwin()
        exit()
import feedparser

class getfeed:

    def __init__(self, url):
        self.link = url
        self.a = feedparser.parse(self.link)
    
    def getTitle(self, item):
        return self.a.entries[item].title

    def getMessage(self, item):
        return self.a.entries[item].summary

    def getURL(self, item):
        return self.a.entries[item].link

Also, is there a feedparser-like module for haskell, because i want to learn haskell more then python but i don't know what to code in haskell neutral

Offline

#2 2008-10-05 23:06:23

pointone
Wiki Admin
From: Waterloo, ON
Registered: 2008-02-21
Posts: 379

Re: Python question

>>> u'\xe8'
u'\xe8'
>>> u'\xe8'.encode("ascii")
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    u'\xe8'.encode("ascii")
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in position 0: ordinal not in range(128)
>>> u'\xe8'.encode("ascii", "replace")
'?'
>>>

Not sure where exactly your unicode-to-ASCII conversion is taking place, but I ran into this same problem not long ago. By default, the "encode" error-handling mode will raise a UnicodeDecodeError/UnicodeEncodeError.

>>> help("".encode)
Help on built-in function encode:

encode(...)
    S.encode([encoding[,errors]]) -> object
    
    Encodes S using the codec registered for encoding. encoding defaults
    to the default encoding. errors may be given to set a different error
    handling scheme. Default is 'strict' meaning that encoding errors raise
    a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and
    'xmlcharrefreplace' as well as any other name registered with
    codecs.register_error that is able to handle UnicodeEncodeErrors.

>>>

Hope this helps.


M*cr*s*ft: Who needs quality when you have marketing?

Offline

Board footer

Powered by FluxBB