1

I'm very new to python and I have a mix of both ansi and unicode (utf-16-le) text based files in a series of directories. I've got some code which reads the text files okay until it hits a unicode file which at the mo, I've written in the code to skip. . I'm wondering if there's anyway I can get python to run a

with codecs.open 

type of thing when it hits a unicode file as part of one prog? With my currrent level of python experience, the only way I could see of doing this is to write two separate progs; one to process the ANSI stuff and one for the Unicode.

Thanks in advance for any help you can provide

2 Answers 2

1

use unicode by default(which is a good programming discipline) and switch to ansi only if necessary.

import codecs def opener(filename): try: f = codecs.open(filename, encoding='utf-8') except UnicodeError: f = open(filename) return f 
Sign up to request clarification or add additional context in comments.

Comments

0

Just open all files using UTF-8.

f = codecs.open(file_name, "r", "utf-8") 

1 Comment

Thanks. I've tried that and I get a utf8 can't decode byte 0xac in position 26449:

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.