Re: catch UnicodeDecodeError

2012-07-26 Thread jaroslav . dobrek
> that tells you the exact code line where the error occurred. No need to > look around. You are right: try: for line in f: do_something() except UnicodeDecodeError: do_something_different() does exactly what one would expect it to do. Thank you very much for pointing this out

Re: catch UnicodeDecodeError

2012-07-26 Thread Jaroslav Dobrek
On Jul 26, 12:19 pm, wxjmfa...@gmail.com wrote: > On Thursday, July 26, 2012 9:46:27 AM UTC+2, Jaroslav Dobrek wrote: > > On Jul 25, 8:50 pm, Dave Angel <d...@davea.name> wrote: > > > On 07/25/2012 08:09 AM, jarosla

Re: catch UnicodeDecodeError

2012-07-26 Thread Jaroslav Dobrek
> And the cool thing is: you can! :) > > In Python 2.6 and later, the new Py3 open() function is a bit more hidden, > but it's still available: > >     from io import open > >     filename = "somefile.txt" >     try: >         with open(filename, encoding="utf-8") as f: >             for line in f:

Re: catch UnicodeDecodeError

2012-07-26 Thread Jaroslav Dobrek
On Jul 25, 8:50 pm, Dave Angel wrote: > On 07/25/2012 08:09 AM, jaroslav.dob...@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 pr

Re: catch UnicodeDecodeError

2012-07-25 Thread jaroslav . dobrek
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').

catch UnicodeDecodeError

2012-07-25 Thread jaroslav . dobrek
Hello, very often I have the following problem: I write a program that processes many files which it assumes to be encoded in utf-8. Then, some day, I there is a non-utf-8 character in one of several hundred or thousand (new) files. The program exits with an error message like this: UnicodeDec

Re: parallel subprocess.getoutput

2012-05-14 Thread Jaroslav Dobrek
Sorry, for code-historical reasons this was unnecessarily complicated. Should be: MY_DIR = '/my/path/to/dir' FILES = os.listdir(MY_DIR) def grep(regex): output = [] for f in FILES: command = "egrep " + '"' + regex + '" ' + MY_DIR + '/' + f result = subprocess.getoutput(c

parallel subprocess.getoutput

2012-05-14 Thread Jaroslav Dobrek
Hello, I wrote the following code for using egrep on many large files: MY_DIR = '/my/path/to/dir' FILES = os.listdir(MY_DIR) def grep(regex): i = 0 l = len(FILES) output = [] while i < l: command = "egrep " + '"' + regex + '" ' + MY_DIR + '/' + FILES[i] result = s

system call that is killed after n seconds if not finished

2012-04-16 Thread Jaroslav Dobrek
Hello, I would like to execute shell commands, but only if their execution time is not longer than n seconds. Like so: monitor(os.system("do_something"), 5) I.e. the command do_somthing should be executed by the operating system. If the call has not finished after 5 seconds, the process should b

subtraction of floating point numbers

2012-02-24 Thread Jaroslav Dobrek
Hello, when I have Python subtract floating point numbers it yields weird results. Example: 4822.40 - 4785.52 = 36.87992 Why doesn't Python simply yield the correct result? It doesn't have a problem with this: 482240 - 478552 = 3688 Can I tell Python in some way to do this differently?

read from file with mixed encodings in Python3

2011-11-07 Thread Jaroslav Dobrek
Hello, in Python3, I often have this problem: I want to do something with every line of a file. Like Python3, I presuppose that every line is encoded in utf-8. If this isn't the case, I would like Python3 to do something specific (like skipping the line, writing the line to standard error, ...) L