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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:47:47+00:00 2026-05-26T15:47:47+00:00

I have the following function: def storeTaggedCorpus(corpus, filename): corpusFile = codecs.open(filename, mode = ‘w’,

  • 0

I have the following function:

def storeTaggedCorpus(corpus, filename):
    corpusFile = codecs.open(filename, mode = 'w', encoding = 'utf-8')
    for token in corpus:
        tagged_token = '/'.join(str for str in token)
        tagged_token = tagged_token.decode('ISO-8859-1')
        tagged_token = tagged_token.encode('utf-8')
        corpusFile.write(tagged_token)
        corpusFile.write(u"\n")
    corpusFile.close()

And when I execute it, I’ve got the following error:

(...) in storeTaggedCorpus
    corpusFile.write(tagged_token)
  File "c:\Python26\lib\codecs.py", line 691, in write
    return self.writer.write(data)
  File "c:\Python26\lib\codecs.py", line 351, in write
    data, consumed = self.encode(object, self.errors)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)

So i went to debug it, and discovered that the created file was encoded as ANSI, not UTF-8 as declared in corpusFile = codecs.open(filename, mode = 'w', encoding = 'utf-8'). If the
corpusFile.write(tagged_token) is removed, this function will (obviously) work, and the file will be encoded as ANSI. If instead I remove tagged_token = tagged_token.encode('utf-8'), it will also work, BUT the resulting file will have encoding "ANSI as UTF-8" (???) and the latin characters will be mangled. Since I’m analizing pt-br text, this is unacceptable.

I believe that everything would work fine if the corpusFile opened as UTF-8, but I can’t get it to work. I’ve searched the Web, but everything I found about Python/Unicode dealt with something else…s So why this file always ends up in ANSI? I am using Python 2.6 in Windows 7 x64, and those file encodings were informed from Notepad++.

Edit — About the corpus parameter

I don’t know the encoding of the corpus string. It was generated by PlaintextCorpusReader.tag() method, from NLTK. The original corpus file was encoded in UTF-8, according to Notepad++. The tagged_token.decode('ISO-8859-1') is just a guess. I’ve tried to decode it as cp1252, and got the same mangled characters from ISO-8859-1.

  • 1 1 Answer
  • 3 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-26T15:47:48+00:00Added an answer on May 26, 2026 at 3:47 pm

    When you open the file with codec.open('w', encoding='utf8'), there is no point in writing byte arrays (str objects) into the file. Instead, write unicode objects, like this:

    corpusFile = codecs.open(filename, mode = 'w', encoding = 'utf-8')
    # ...
    tagged_token = '\xdcml\xe4ut'
    tagged_token = tagged_token.decode('ISO-8859-1')
    corpusFile.write(tagged_token)
    corpusFile.write(u'\n')
    

    This will write platform-dependent End-Of-Line characters.

    Alternatively, open a binary file and write byte arrays of already-encoded strings:

    corpusFile = open(filename, mode = 'wb')
    # ...
    tagged_token = '\xdcml\xe4ut'
    tagged_token = tagged_token.decode('ISO-8859-1')
    corpusFile.write(tagged_token.encode('utf-8'))
    corpusFile.write('\n')
    

    This will write platform-independent EOLs. If you want a platform-dependent EOL, print os.sep instead of '\n'.

    Note that the encoding naming in Notepad++ is misleading: ANSI as UTF-8 is what you want.

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

Sidebar

Related Questions

I have the following python code using the twisted API. def function(self,filename): def results(result):
In python I have the following function: def is_a_nice_element(element, parameter): #do something return True
I have the following function: >>> def rule(x): ... rule = bin(x)[2:].zfill(8) ... rule
Let's say I have the following function: def f(): if TESTING: # Run expensive
I have the following function in my script at the minute: def _convert_time(p): Converts
I have the following view function in activities.views : def activity_thumbnail(request, id): pass I'm
Is filter/map equivalent to list comprehension? Suppose I have the following function def fib_gen():
I have the following function in Ruby that decrypts a bit of data: def
I have the following function: def heading_positions(self): return map( lambda h: {'{t}.{c}'.format(t=h.table_name,c=h.column_name) : h.position
I have the following function: function calculateAspect(options){ def = { orgw: 0, orgh: 0,

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.