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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:51:34+00:00 2026-06-05T19:51:34+00:00

I have a file, which I read from test . This file is UTF-8.

  • 0

I have a file, which I read from test. This file is UTF-8. It contains, in my simple example, only the Danish letter “Ø”.

I then have a Python script, which reads this file, and in this example, just prints every line.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import codecs
import sys

filename = sys.argv[1]

f = codecs.open(filename, 'r', 'utf-8')

for lines in f:
  print lines

Call this parse.py. Now when I run ./parse.py test in my terminal I get the following output:

Ø

Calling instead ./parse.py test | less gives me:

Traceback (most recent call last):
  File "./test.py", line 12, in <module>
    print lines
UnicodeEncodeError: 'ascii' codec can't encode character u'\xd8' in position 11: ordinal not in range(128)

I am certain my test file is ‘UTF-8’:

$ file -I test
test: text/plain; charset=utf-8

As well as my $LC_TYPE being ‘UTF-8’

What am I doing wrong? How do I get it to work, so that I can pass the output of parse.py to the next command?

  • 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-05T19:51:36+00:00Added an answer on June 5, 2026 at 7:51 pm

    This is probably a problem with less, see this article for some tips. Maybe changing the configuration of less will help.

    If your system supports the UTF-8 encoding of Unicode for non-ASCII text, as many modern systems do, you should either set your locale to something that includes the string “UTF-8” or “UTF8” (either uppercase or lowercase is ok), or set LESSCHARSET to “utf-8”.

    Ok, this wasn’t the problem…so updating the answer based on the comments.
    Needed to encode the string before print. This article gives the reason, summed up: python needs to be told how to encode the unicode.

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

Sidebar

Related Questions

I have a file from which i read line by line and i have
I have dynamic array filled with bytes, which are read from .raw file with
I have a char array in Python, which is read from a file e.g.
I read file from user using FileReference. I have a variable which I want
I have an xml file(from federal government's data.gov) which I'm trying to read with
I have this file which I need to read the first bytes to check
I have a ruby script which will read data from some CSV file and
I have a VB web application that reads from a CSV file which contains
I have a text file which is read in. It has delimeters which are
I have a sed script which will read in a file and, if a

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.