How to Use unicode with a list or a string in Python
Question: So i Have a list of some Irish(gaelic words) words that I want to use the
unicode with so that RDFlib will be able to understand the accents above some
of the letters in the word. I dont know whether to use the unicode before the
words are in the list or after. Here is the code I have so far:

sample line in file = `00001740 n 3 eintiteas aonán beith 003 ~ 00001930 n
0000`

    
    
    def process_file(self):
        self.file = open("testing_line_ir.txt", "r")
        return self.file
    
    def line_for_loop(self, file):
        for line in file:
            self.myline = unicode(line, 'utf-8')
            for line in self.myline:
            ............here is where other processes are ran.......
    

This is giving out the error:

    
    
    UnicodeDecodeError: 'utf8' codec can't decode byte 0xe1 in position 26: invalid continuation byte
    

and i have also tried this:

    
    
    def get_words_list(self, word_part, num_words):
        self.word = word_part[3:3 + num_words:1]
        self.myword = [unicode(i) for i in self.word]
        return self.myword
    

In this case, 'word' is the list of words ['eintiteas', 'aonán', 'beith'] and
I tried using myword as the encoded list, with the same error as above.

EDIT: Here is the source code from where the error occurs, it occurs on the
graph.parse line The variables passing through like block1 and namespaces are
just lines of text

    
    
    def compose_printout(self, namespaces, block1, block2, close_rdf):
        self.printout += namespaces + block1 + block2 + close_rdf
        self.tabfile = StringIO(self.printout)
        return self.tabfile
    
    def serialize(self, graph, tabfile):
        """ This will serialize with RDFLib """
        graph.parse(tabfile, publicID=None, format="xml")
    

Some of these words are getting added to a RDFlib graph so any help here would
be great!!


Answer: You do not have UTF-8 data. From the exception message I'd say you have
Latin-1 encoded data instead:

    
    
    >>> print '\xe1'.decode('latin1')
    á
    

You can use the [`codecs.open()`
function](http://docs.python.org/2/library/codecs.html#codecs.open) to create
a file object that returns file data ready-decoded:

    
    
    import codecs
    
    def process_file(self):
        self.file = codecs.open("testing_line_ir.txt", "r", 'latin-1')
        return self.file
    
    def line_for_loop(self, file):
        for line in file:
            # line is *already* unicode
    

