d at davea.name
Wed Jul 25 20:50:18 CEST 2012
On 07/25/2012 08:09 AM, jaroslav.dobrek at gmail.com wrote:
> On Wednesday, July 25, 2012 1:35:09 PM UTC+2, Philipp Hagemeister wrote:
>> Hi Jaroslav,
>> you can catch a UnicodeDecodeError just like any other exception. Can
>> you provide a full example program that shows your problem?
>> This works fine on my system:
>> import sys
>> open('tmp', 'wb').write(b'\xff\xff')
>> buf = open('tmp', 'rb').read()
>> except UnicodeDecodeError as ude:
>> sys.exit("Found a bad char in file " + "tmp")
> Thank you. I got it. What I need to do is explicitly decode text.
> But I think trial and error with moving files around will in most cases be faster. Usually, such a problem occurs with some (usually complex) program that I wrote quite a long time ago. I don't like editing old and complex programs that work under all normal circumstances.
> What I am missing (especially for Python3) is something like:
> for line in sys.stdin:
> except UnicodeDecodeError:
> sys.exit("Encoding problem in line " + str(line_number))
> I got the point that there is no such thing as encoding-independent lines. But if no line ending can be found, then the file simply has one single line.
i can't understand your question. if the problem is that the system
doesn't magically produce a variable called line_number, then generate
it yourself, by counting
in the loop.
Don't forget that you can tell the unicode decoder to ignore bad
characters, or to convert them to a specified placeholder.
More information about the Python-list