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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:24:43+00:00 2026-06-17T18:24:43+00:00

I am running the following code: def displayFileOld(file_path): f = open(file_path, mode = ‘rt’,

  • 0

I am running the following code:

def displayFileOld(file_path):
    f = open(file_path, mode = 'rt', encoding = 'cp1252', errors = 'replace')  
    while True:
        line = f.readline()
        if len(line) == 0:
            break
        print(line)

under Python 3.3, Windows 8 Pro.

The file that I am “parsing” (Java source file) is shown by Eclipse as being encoded in Cp1252 (“inherited from the main container”). Notepad++ says nothing more under the Encoding menu than “ANSI”. These two match.

First of all, I would expect the encoding to Unicode to…work. It fails, though, with the message:

Traceback (most recent call last):
  File "C:\work\test.py", line 69, in <module>
    main()
  File "C:\work\test.py", line 65, in main
    displayFileOld(r'C:\work\CVSProvisioningFeatures.java')
  File "C:\work\test.py", line 48, in displayFileOld
    print(line)
  File "C:\Python33\lib\encodings\cp437.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 62-63:     character maps to <undefined>

Second, I wouldn’t expect to have my stack trace mention cp437.py, instead of the *.py file corresponding to the encoding I have mentioned in the flag. (The parsing fails when the “†” character is encountered, not sure how Unicode would not include this one – this is the context: ‘new FeatureDescription(i++,”†† “+str));’).

Third, I am not sure why the errors flag is ignored altogether.

I have spent a few hours trying the different encodings that are hosted under the generic “ANSI” umbrella, but in vain. All I can do is catch the exception and ignore the line (not acceptable). Another approach is to use some “exotic” encoding such as MacRoman, but that still leaves me with some unexpected characters (albeit I get 12 errors only instead of 431) after going through the whole source tree…characters that I will ultimately need to forward work with, passing tons of strings around. I have about 50 MB of Java sources to work on using a script, so any help getting this set up would be greatly appreciated.

  • 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-17T18:24:44+00:00Added an answer on June 17, 2026 at 6:24 pm

    Your problem is not with reading the file, but with printing; the traceback shows that the line print(line) preceeds the UnicodeEncodeError (note the Encode in that exception). When you read a file, you are decoding from cp1252 to unicode objects, and that is working just fine.

    Your windows terminal is using codepage 437 and cannot handle the characters you are trying to print. Python needs to convert your data from unicode to whatever your terminal is using to be able to display the characters to you.

    You can switch your terminal codepage with the chcp 65001 command (not a Python expresssion but a Windows commandline tool). Codepage 65001 is the UTF-8 codepage, which can handle all Unicode code points. You may need to switch fonts to be able to display these characters too. See Unicode characters in Windows command line – how?

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

Sidebar

Related Questions

I'm getting an error while running the following code: class Person: def _init_(self, name):
while running following code : def v1=[]; def p=[]; v.as('x') .except(v1).sideEffect{v1.add(it)} // step 1
On running the following code in groovy - import groovy.time.* import org.codehaus.groovy.runtime.TimeCategory def today
Try running the following code: class Test(object): def func_accepting_args(self,prop,*args): msg = %s getter/setter got
I have the following code: def application(env, start_response): path = process(env) fh = open(path,'r')
I have the following code: require 'spreadsheet' class MyObject def initialize @workbook = Spreadsheet::open('foo.xls',
I am running following code to create bmp image from pdf using Ghost4j i
I'm getting unexpected output when running following code, DateFormat df = new SimpleDateFormat(YYYY-MM-dd); Date
Running the following code, I get a StackOverflowError at the getPackage() line. How can
Running the following code, I get an exception: using (var client = new Pop3Client())

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.