0

Hi I ran into a problem when trying to process the information in a series of Qlineedits. Im living in Norway and the characters "æøå" arent accepted by my script.

This is the failing code. Retrieves the information of all my lineinputs and stores the information in a dictionary for further processing and placement in a database.

for navn in inputs:
    a = unicode(str(navn.text() ) )
    b = unicode(str(navn.objectName() ) )
    bibliotek[navn.objectName()] = a
    navn.clear()

The error I return when typing a non-ascii characters is as follows.

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

I am working in python 2.7 and I have of cource made the whole document utf-8. What do I do to solve the problem? Any answer is aprechiated.

  • `unicode(str(navn.text() ) )` ? What happens if you remove the type casting and just have `a = navn.text()`, etc? – NightShadeQueen Jun 28 '15 at 17:21
  • Never use `str` to convert `QString`. Just do, e.g. `a = unicode(navn.text())`. – ekhumoro Jun 28 '15 at 18:59
  • It still wont work. The only diffenrence when removing the str() is that the error is now located when the text is inserted into a sql-query. – Martin Klingenberg Jun 30 '15 at 23:35
  • @MartinKlingenberg. Of course it *does* work, as you've now proved for yourself. However, you made no mention of sql-queries, which is an entirely different question. Your mistake is that you keep trying to mix unicode and bytes. You can get away with this as long as they both only contain ascii. But if they don't, you first have to either decode the bytes to unicode, or encode the unicode to bytes. – ekhumoro Jul 01 '15 at 23:59
  • Well it is not the sql-query that fails. the error lies where I create the query in a variable. query = "The query with some none-ascii characters" and then I get an error. I know about the issues with non-ascii and sql, sortet that out. – Martin Klingenberg Jul 03 '15 at 00:51

0 Answers0