Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6929897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:22:36+00:00 2026-05-27T11:22:36+00:00

I have a list with some strings (most of which I fetched from a

  • 0

I have a list with some strings (most of which I fetched from a sqlite3 database):

stats_list = ['Statistik \xc3\xb6ver s\xc3\xa5nger\n', 'Antal\tS\xc3\xa5ng', '1\tCarola - Betlehems Stj\xc3\xa4rna', '\n\nStatistik \xc3\xb6ver datak\xc3\xa4llor\n', 'K\xc3\xa4lla\tAntal', 'MANUAL\t1', '\n\nStatistik \xc3\xb6ver \xc3\xb6nskare\n', 'Antal\tId', u'1\tNiclas']

When I try to join it with:

return '\n'.join(stats_list)

I get this error:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 10: ordinal not in range(128)

Is it possible to get any clue why this happens just by looking at the list? If I loop over the list and print it to screen, I get this:

Statistik över sånger

Antal   Sång 
1   Carola - Betlehems Stjärna


Statistik över datakällor

Källa   Antal 
MANUAL  1


Statistik över önskare

Antal   Id
1   Niclas

which is exactly what I was expecting, and no error is shown. (The special characters are swedish).

EDIT:

I’ll tried this:

   return '\n'.join(i.decode('utf8') for i in stats_list)

But it returned:

Traceback (most recent call last):
  File "./CyberJukebox.py", line 489, in on_stats_to_clipboard
    stats = self.jbox.get_stats()
  File "/home/nine/dev/python/CyberJukebox/jukebox.py", line 235, in get_stats
    return self._stats.get_string()
  File "/home/nine/dev/python/CyberJukebox/jukebox.py", line 59, in get_string
    return '\n'.join(i.decode('utf8') for i in stats_list)
  File "/home/nine/dev/python/CyberJukebox/jukebox.py", line 59, in <genexpr>
    return '\n'.join(i.decode('utf8') for i in stats_list)
  File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 10: ordinal not in range(128)

EDIT 2:

The suggested solution works for me in the interpreter. But when I execute the code it won’t work. I can’t wrap my head around this. Maybe it’s something obvious I’m missing so I’m pasting the whole method here:

 def get_string(self):
     stats_list = [u'Statistik över sånger\n', u'Antal\tSång']
     stats = sorted([(v, k) for k, v in self.song_stats.iteritems()], reverse=True)
     for row in stats:
         line = '%s\t%s' % row
         stats_list.append(line)

     stats_list.append(u'\n\nStatistik över datakällor\n')
     stats_list.append(u'Källa\tAntal')
     stats = sorted([(k, v) for k, v in self.exts_stats.iteritems()])
     for row in stats:
         line = '%s\t%s' % row
         stats_list.append(line)

     stats_list.append(u'\n\nStatistik över önskare\n')
     stats_list.append(u'Antal\tId')
     stats = sorted([(v, k) for k, v in self.wisher_stats.iteritems() if k != ''], reverse=True)
     for row in stats:
         line = '%s\t%s' % row
         stats_list.append(line)

     return '\n'.join(i.decode('utf8') for i in stats_list)

song_stats, exts_stats and wisher_stats are dictionaries in the class.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:22:36+00:00Added an answer on May 27, 2026 at 11:22 am

    Your problem is probably that you are mixing unicode strings with byte strings.

    The code in “Edit 2” has several unicode strings being added to stats_list:

    stats_list = [u'Statistik över sånger\n', u'Antal\tSång']
    

    If you try to decode these unicode strings, you will get a UnicodeEncodeError. This because Python will first try to use the default encoding (usually “ascii”) to encode the strings before trying to decode them. It only ever makes sense to decode byte strings.

    So to start with, change the final line in the function to:

    return '\n'.join(stats_list)
    

    Now you need to check whether any of the other strings that get added to stats_list are byte strings, and ensure they get decoded to unicode strings properly first.

    So put print type(line) after the three lines like this:

    line = '%s\t%s' % row
    

    and then wherever it prints <type 'str'>, change the following line to:

    stats_list.append(line.decode('utf-8'))
    

    Of course, if it prints <type 'unicode'>, there’s no need to change the following line.

    A even better solution here would be to check how the dictionaries song_stats, exts_stats and wisher_stats are created, and make sure they always contain unicode strings (or byte strings that only contain ascii characters).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some LINQ code that generates a list of strings, like this: var
I have a list which contains some items of type string. List<string> lstOriginal; I
I have a list that has some chapter numbers in string. When I sort
Some open source libraries have tendency to re implement basic structures like string, list,
I have a list with some lis. <ul> <li id=eup>....</li> <li id=1>a</li> <li id=2>b</li>
I have list of images with some simple mouseenter / mouseleave effect. http://jsfiddle.net/4vTCr/ I
I have list of catalog paths and need to filter out some of them.
Say you have a std::list of some class. There are two ways you can
I am trying to output some Java objects as JSON, they have List properties
I have some std::list<char> list_type Now I have to supply contents of the list

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.