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

  • Home
  • SEARCH
  • 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

I am not able to replace the string (-) using re.sub in Python. >>>
How can I remove duplicate characters from a string using Python? For example, let's
I know I can slice a string in Python by using array notation: str[1:6]
What does Python's string.replace return if no string substitution was made? Does Python's file.open(f,
i need to check existing of Character (*,&,$) in Given String using python command
Using Python I want to randomly rearrange sections of a string based on a
Using Python I need to insert a newline character into a string every 64
Using Python I need to delete all characters in a multiline string up to
I am using Python 2.7 . I have an alphanumeric string, on which I
I'm using python 2.3.2. In script I want to split a string based on

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.