Py3 - converting bytes to ascii

2009-01-15 Thread Anjanesh Lekshminarayanan
Using Python 3.0 res = urllib.request.urlopen(url) f = open('file.txt', 'wb') # Since res.read() returns bytes f.write(res.read()) But newline and return feeds are stored as b14, 58a as text in the text file. So how do I to convert res.read() to ascii on opening the file in ascii mode f = open('

Re: Py3 - converting bytes to ascii

2009-01-15 Thread Anjanesh Lekshminarayanan
The problem seems be solved with urllib.request.urlretrieve() I think the binary information read() was giving had headers like content-size - but not HTTP headers. The first couple of bytes indicate how much content to read and after reading that content, the next set of bytes indicate the next

Method returning an Iterable Object

2009-01-26 Thread Anjanesh Lekshminarayanan
Is there a way to return an iterable object ? class twoTimes: def __init__(self, n): self.__n = n def getNext(): self.__n *= 2 return self.__n t = twoTimes(5) while (n in t.getNext()): # while (n in t): print (n) -- Anjanesh Lekshmnarayanan -- http://mail.p

Re: Method returning an Iterable Object

2009-01-26 Thread Anjanesh Lekshminarayanan
> You can also replace the whole class with a function thusly: > >def two_times(n): >for k in itertools.count(1): >yield n * (2**k) > > This function is then called a generator (because it generates an > iterator). You can now say > >infinitely_doubling_numbers = two_tim

Re: Method returning an Iterable Object

2009-01-26 Thread Anjanesh Lekshminarayanan
But how come a raise StopIteration in the next() method doesnt need to be caught ? It works without breaking. class twoTimes: max = 10**10 def __init__(self, n): self.__n = n def next(self): if self.__n > self.max: raise StopIteration self.__n *= 2

UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 10442: character maps to

2009-01-29 Thread Anjanesh Lekshminarayanan
Im reading a file. But there seems to be some encoding error. >>> f = open(filename) >>> data = f.read() Traceback (most recent call last): File "", line 1, in data = f.read() File "C:\Python30\lib\io.py", line 1724, in read decoder.decode(self.buffer.read(), final=True)) File "C:\P

Re: UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 10442: character maps to

2009-01-29 Thread Anjanesh Lekshminarayanan
> It does auto-detect it as cp1252- look at the files in the traceback and > you'll see lib\encodings\cp1252.py. Since cp1252 seems to be the wrong > encoding, try opening it as utf-8 or latin1 and see if that fixes it. Thanks a lot ! utf-8 and latin1 were accepted ! -- http://mail.python.org/mail

Re: mod_python resources

2008-12-19 Thread Anjanesh Lekshminarayanan
Same requirement here. But isnt there any mod_python for Python 3.0 ? Or do we need to build it from source ourselves ? I was hoping there would be mod_wsgi binaries for Python 3.0. -- http://mail.python.org/mailman/listinfo/python-list

Multiple conditional expression

2009-02-26 Thread Anjanesh Lekshminarayanan
How do I achieve something like this using python ? spaces = (form.has_key('spaces') ? form.getvalue('spaces') == 1 ? True : False : False) spaces = True if form.getvalue('spaces') == 1 if form.has_key('spaces') else False else False -- Anjanesh Lekshmnarayanan -- http://mail.python.org/mailman/l

Re: UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 10442: character maps to

2009-02-26 Thread Anjanesh Lekshminarayanan
> (1) what is produced on Anjanesh's machine >>> sys.getdefaultencoding() 'utf-8' > (2) it looks like a small snippet from a Python source file! Its a file containing just JSON data - but has some unicode characters as well as it has data from the web. > Anjanesh, Is it a .py file Its a .json fil

Re: Multiple conditional expression

2009-02-26 Thread Anjanesh Lekshminarayanan
> How do we know that from the what the OP posted? Its CGI alright. spaces = form.has_key('spaces') and form.getvalue('spaces') == '1' But I just dont see how spaces = (form.has_key('spaces') ? form.getvalue('spaces') == 1 ? True: False : False) is complicated in anyway. Its not that hard to read

array next pointer

2009-03-17 Thread Anjanesh Lekshminarayanan
>>> a = ['cat','dog','elephant'] >>> a.next() Traceback (most recent call last): File "", line 1, in AttributeError: 'list' object has no attribute 'next' >>> Is there something that imtates PHP's next() ? (http://php.net/next) -- http://mail.python.org/mailman/listinfo/python-list

EOF

2008-08-22 Thread Anjanesh Lekshminarayanan
|Hi Im trying to download a file from a server. But how do I detect EOF ? || import urllib2 f1 = urllib2.urlopen('ftp://username:[EMAIL PROTECTED]/data.zip') f2 = file("data.zip", "wb") while f1: # When to stop ? f2.write(f1.read(1024)) f1.close() f2.close() || I can get the size & us

Re: EOF

2008-08-24 Thread Anjanesh Lekshminarayanan
Thanks for the shutil.copyfileobj. Oddly, the EOFError didnt work though. Gabriel Genellina wrote: En Fri, 22 Aug 2008 16:53:58 -0300, Wojtek Walczak <[EMAIL PROTECTED]> escribió: On Fri, 22 Aug 2008 22:18:37 +0530, Anjanesh Lekshminarayanan wrote: Im trying to download a fil

PHP's str_replace ?

2008-09-10 Thread Anjanesh Lekshminarayanan
In PHP, if I do str_replace(array('a', 'e', 'i', 'o', 'u'), '-', $str) it'll replace all vowels with a hyphen in string $str. Is there some equivalent in Python ? Thanks -- http://mail.python.org/mailman/listinfo/python-list

Re: PHP's str_replace ?

2008-09-10 Thread Anjanesh Lekshminarayanan
Matthias Huening wrote: Matthias Huening (10.09.2008 16:07): Anjanesh Lekshminarayanan (10.09.2008 15:50): In PHP, if I do str_replace(array('a', 'e', 'i', 'o', 'u'), '-', $str) it'll replace all vowels with a hyphen in string $

print function in python3.1

2009-11-23 Thread Anjanesh Lekshminarayanan
Python 3.1.1 sql = "INSERT INTO `tbl` VALUES (NULL, '%s', '%s', '%s', '%s', '%s');" for row in fp: print (sql, (row[0],row[1],row[2],row[3],row[4])) . INSERT INTO `tbl` VALUES (NULL, '%s', '%s', '%s', '%s', '%s'); ('142', 'abc', '2006-04-09 02:19:24', '', '') . Why is it showing %s in the outp

Re: print function in python3.1

2009-11-23 Thread Anjanesh Lekshminarayanan
> Depending on your DB-adapter, you are out of luck here. Either connect to a > db even if you don't need it, or try & see if you can locate the > implementation in the module somehow. ImportError: No module named MySQLdb MySQLdb only available in Python2. -- Anjanesh Lekshmnarayanan -- http:

Re: print function in python3.1

2009-11-23 Thread Anjanesh Lekshminarayanan
As of now, there is no mysql adaptor for Python3. Hence cant use escape_string() > I don't have the slightest clue what you want to say with that. -- Anjanesh Lekshmnarayanan -- http://mail.python.org/mailman/listinfo/python-list

Re: print function in python3.1

2009-11-23 Thread Anjanesh Lekshminarayanan
> Maybe it would help if you explained what you are actually trying to > accomplish. import csv f = csv.reader(open('data.txt'), delimiter='\t') # 2GB text file sql = "INSERT INTO `data` VALUES (NULL,%s,%s,%s,%s,%s);" for row in f: print (sql, (row[0],row[1],row[2],row[3],row[4])) $ python3 p

Waiting for receiving data

2009-11-23 Thread Anjanesh Lekshminarayanan
fp = urllib.urlopen(url) data = fp.read() Retrieving XML data via an XML service API. Very often network gets stuck in between. No errors / exceptions. CTRL+C File "get-xml.py", line 32, in fp = urllib.urlopen(url) File "/usr/lib/python2.6/urllib.py", line 87, in urlopen return open

Integer Division

2009-06-19 Thread Anjanesh Lekshminarayanan
>>> a = 1 >>> b = 25 >>> a / b 0 >>> float(a) / b 0.040001 >>> >>> from __future__ import division >>> a = 1 >>> b = 25 >>> a / b 0.040001 >>> In what simple way can I get just 0.04 ? -- Anjanesh Lekshmnarayanan -- http://mail.python.org/mailman/listinfo/python-list