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 8235133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:42:08+00:00 2026-06-07T18:42:08+00:00

Does anyone have any experience with this? I have been using python 3.2 for

  • 0

Does anyone have any experience with this?

I have been using python 3.2 for the last half a year, and my memory of 2.6.2 is not that great.

On my computer the following code works, tested using 2.6.1:

import contextlib
import codecs

def readfile(path):
    with contextlib.closing( codecs.open( path, 'r', 'utf-8' )) as f:
        for line in f:
            yield line

path = '/path/to/norsk/verbs.txt'

for i in readfile(path):
    print i

but on the phone it gets to the first special character ø and throws:

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

any ideas as I am going to need to input them as well as read form a file?

  • 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-06-07T18:42:09+00:00Added an answer on June 7, 2026 at 6:42 pm

    Printing is an I/O operation. I/O requires bytes. What you have in i is unicode, or characters. Characters only convert directly to bytes when we’re talking about ascii, but on your phone you have encountered a non-ascii character (u’\xf8′ is ø). To convert characters to bytes, you need to encode them.

    import contextlib
    import codecs
    
    def readfile(path):
        with contextlib.closing( codecs.open( path, 'r', 'utf-8' )) as f:
            for line in f:
                yield line
    
    path = '/path/to/norsk/verbs.txt'
    
    for i in readfile(path):
        print i.encode('utf8')
    

    As to why this works on your code works on one machine and not the other, I bet python’s autodetection has found different things in those cases. Run this on each device:

    $ python
    >>> import sys
    >>> sys.getfilesystemencoding()
    'UTF-8'
    

    I expect you’ll see utf8 on one and ascii on the other. This is what print uses when the destination is a terminal. If you’re sure that all users of your python installation (very possibly just you) prefer utf8 over ascii, you can change the default encoding of your python installation.

    1. Find your site.py: python -c 'import site; print site
    2. Open it and find the setencoding function:

      def setencoding(): 
          """Set the string encoding used by the Unicode implementation.  The 
          default is 'ascii', but if you're willing to experiment, you can 
          change this.""" 
          encoding = "ascii" # Default value set by _PyUnicode_Init() 
      
    3. Change the encoding = "ascii" line to encoding = "UTF-8"

    Enjoy as things Just Work. You can find more information on this topic here: http://blog.ianbicking.org/illusive-setdefaultencoding.html

    If you’d instead like a strict separation of bytes vs characters such as python3 provides, you can set encoding = "undefined". The undefined codec will “Raise an exception for all conversions. Can be used as the system encoding if no automatic coercion between byte and Unicode strings is desired.“

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

Sidebar

Related Questions

Does anyone have any experience with this sort of thing? I'm talking about applets
Does anyone have any experience using PHP to send an sms via skype I
Does anyone have any experience using the basic analytics services with the Flurry Analytics
Does anyone have any experience querying FlexLM? (At a minimum) I need to be
Does anyone have any experience with the following fluent config error? faultString = An
Does anyone have any experience with doing mail merge from Java on a word
Does anyone have any experience of storing data in JavaScript across all mobile platforms
Does anyone of you have any experience with switching from SQL Server 2008 to
Does anyone have any good advice or experience on how to create an engine
Does anyone have any experiences from recent daily build of AnkhSVN? Please let me

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.