Python string replace for UTF-16-LE file -


python 2.6

using python string.replace() seems not working utf-16-le file. think of 2 ways:

  1. find python module can handle unicode string manipulate.
  2. convert target unicode file ascii, use string.replace(), convert back. worry may cause loss data.

can community suggest me way solve this? thanks.

edit: code looks this:

infile = open(inputfilename) s in infile:  outfile.write(s.replace(targettext, replacetext)) 

looks loop can parse line correct. did make mistakes here?

edit2:

i've read python unicode tutorial , tried below code, , worked. however, wondering if there's better way this. can help? thanks.

infile = codecs.open(infilename,'r', encoding='utf-16-le')  newlines = [] line in infile:     newlines.append(line.replace(originaltext,replacementtext))  outfile = codecs.open(outfilename, 'w', encoding='utf-16-le') outfile.writelines(newlines) 

do need close infile or outfile?

you don't have unicode file. there no such thing (unless author of notepad, conflates "unicode" , "utf-16le").

please read python unicode howto , joel on unicode.

update i'm glad suggested reading helped you. here's better version of code:

infile = codecs.open(infilename,'r', encoding='utf-16-le') outfile = codecs.open(outfilename, 'w', encoding='utf-16-le') line in infile:     fixed_line = line.replace(originaltext,replacementtext)     # no need save output lines in list     outfile.write(fixed_line) infile.close() outfile.close() 

it's habit release resources (e.g. close files) when finished them. more importantly, output files, directory not updated until close file.

read on "with" statement find out better practice file handling.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -