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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:05:44+00:00 2026-05-18T05:05:44+00:00

Python 2.6 Using Python string.replace() seems not working for UTF-16-LE file. I think of

  • 0

Python 2.6

Using Python string.replace() seems not working for UTF-16-LE file. I think of 2 ways:

  1. Find a Python module that can handle Unicode string manipulate.
  2. Convert the target Unicode file to ASCII, use string.replace(), then convert it back. But I am worry about this may cause loss data.

Can the community suggest me a good way to solve this? Thanks.

EDIT:
My code looks like this:

infile = open(inputfilename)
for s in infile:
 outfile.write(s.replace(targetText, replaceText))

Looks like the for loop can parse the line correct. Did I make any mistakes here?

EDIT2:

I’ve read the Python Unicode tutorial and tried below code, and get it worked. However, just wondering if there’s any better way to do this. Can anyone help? Thanks.

infile = codecs.open(infilename,'r', encoding='utf-16-le')

newlines = []
for line in infile:
    newlines.append(line.replace(originalText,replacementText))

outfile = codecs.open(outfilename, 'w', encoding='utf-16-le')
outfile.writelines(newlines)

Do I need to close infile or outfile?

  • 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-18T05:05:45+00:00Added an answer on May 18, 2026 at 5:05 am

    You don’t have a Unicode file. There is no such thing (unless you are the author of NotePad, which conflates “Unicode” and “UTF-16LE”).

    Please read the Python Unicode HOWTO and Joel on Unicode.

    Update I’m glad the suggested reading helped you. Here’s a better version of your code:

    infile = codecs.open(infilename,'r', encoding='utf-16-le')
    outfile = codecs.open(outfilename, 'w', encoding='utf-16-le')
    for line in infile:
        fixed_line = line.replace(originalText,replacementText)
        # no need to save up all the output lines in a list
        outfile.write(fixed_line)
    infile.close()
    outfile.close()
    

    It’s always a good habit to release resources (e.g. close files) immediately when you are finished with them. More importantly, with output files, the directory is usually not updated until you close the file.

    Read up on the “with” statement to find out about even better practice with file handling.

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

Sidebar

Related Questions

Using Python, how would I go about reading in (be from a string, file
In Python, the where and when of using string concatenation versus string substitution eludes
Python has this wonderful way of handling string substitutions using dictionaries: >>> 'The %(site)s
I have recently been working with Python using Komodo Edit and other simpler editors
How can I read the colors of an image with python using google app
I'm using the following method to send mail from Python using SMTP. Is it
I recently wrote a parser in Python using Ply (it's a python reimplementation of
I'm writing a GUI application in Python using wxPython and I want to display
I am calling a WSDL web service from Python using SOAPpy . The call
I've been looking around for a good MVC framework for Python using PyGTK. I've

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.