Re: Unicode problem

2007-04-08 Thread Rehceb Rotkiv
On Sat, 07 Apr 2007 12:46:49 -0700, Gabriel Genellina wrote: > You have to encode the Unicode object explicitely: print > fileString.encode("utf-8") > (or any other suitable one; I said utf-8 just because you read the input > file using that) Thanks! That's a nice little stumbling block for a new

Re: Checking whether list element exists

2007-04-07 Thread Rehceb Rotkiv
Thanks for your many helpful tips! Rehceb Rotkiv -- http://mail.python.org/mailman/listinfo/python-list

Unicode problem

2007-04-07 Thread Rehceb Rotkiv
Please have a look at this little script: #!/usr/bin/python import sys import codecs fileHandle = codecs.open(sys.argv[1], 'r', 'utf-8') fileString = fileHandle.read() print fileString if I call it from a Bash shell like this $ ./test.py testfile.utf8.txt it works just fine, but when I try to p

Re: Checking whether list element exists

2007-04-07 Thread Rehceb Rotkiv
> In general case it won't work, because lists accept negative indexes: > http://docs.python.org/lib/typesseq.html, 3rd note. Yes, I know! I _want_ the "3rd last list element", i.e. list[-3]. But it may be that the list does not have 3 elements. In this case, list[-3] will throw an error, cf.:

Checking whether list element exists

2007-04-07 Thread Rehceb Rotkiv
I want to check whether, for example, the element myList[-3] exists. So far I did it like this: index = -3 if len(myList) >= abs(index): print myList[index] Another idea I had was to (ab-?)use the try...except structure: index = -3 try: print myList[index] except: print

Re: Overlapping matches

2007-04-01 Thread Rehceb Rotkiv
Both methods work well, thank you! -- http://mail.python.org/mailman/listinfo/python-list

Overlapping matches

2007-04-01 Thread Rehceb Rotkiv
In the re documentation, it says that the matching functions return "non- overlapping" matches only, but I also need overlapping ones. Does anyone know how this can be done? Regards, Rehceb Rotkiv -- http://mail.python.org/mailman/listinfo/python-list

Re: Unicode list

2007-04-01 Thread Rehceb Rotkiv
> When printing a list, the individual elements are converted with repr(), > not with str(). For a string object, repr() adds escape codes for all > bytes that are not printable ASCII characters. Thanks Martin, you're right, it were the repr() calls that messed up the output. Iterating the array

Unicode list

2007-03-31 Thread Rehceb Rotkiv
Hello, I have this little grep-like program: ++snip++ #!/usr/bin/python import sys import re pattern = sys.argv[1] inputfile = file(sys.argv[2], 'r') for line in inputfile: matches = re.findall(pattern, line) if matches: print matches ++snip++ L

Re: Sorting a multidimensional array by multiple keys

2007-03-31 Thread Rehceb Rotkiv
Thank you all for your helpful solutions! Regards, Rehceb -- http://mail.python.org/mailman/listinfo/python-list

Re: Sorting a multidimensional array by multiple keys

2007-03-31 Thread Rehceb Rotkiv
Wait, I made a mistake. The correct result would be reaction is BUT by the pattern , BUT it is rapid , BUT it is sea , BUT it is sodium , BUT it is this manner BUT the dissolved because "by the" comes before and "the dissolved" after "it is". Sorry for the confusion. -- http://mail.python.org/m

Re: Sorting a multidimensional array by multiple keys

2007-03-31 Thread Rehceb Rotkiv
> If you want a good answer you have to give me/us more details, and an > example too. OK, here is some example data: reaction is BUT by the sodium , BUT it is sea , BUT it is this manner BUT the dissolved pattern , BUT it is rapid , BUT it is As each line consists of 5 words, I would break up t

Sorting a multidimensional array by multiple keys

2007-03-31 Thread Rehceb Rotkiv
Hello everyone, can I sort a multidimensional array in Python by multiple sort keys? A litte code sample would be nice! Thx, Rehceb -- http://mail.python.org/mailman/listinfo/python-list