Re: [Tutor] Need help with converting script using 2.6's urllib2 to Python 3.1
On 25 October 2010 14:46, Richard D. Moores rdmoo...@gmail.com wrote: I'd like to convert the script to 3.1, but I can't understand the docs for the 3.1 urllib module. Please someone tell me what to do. (Converting 'print rate' to 'print(rate)' I understand.) Have you actually tried reading the documentation? The _very_ first section of the urllib documentation we have urllib.request.urlopen [1]. Which looks to me what you are looking for as a replacement to urllib2.urlopen(). Some *untested* inline comments below. import urllib2 from urllib import request a = urllib2.urlopen('http://www.marketwatch.com/investing/currency/CUR_USDYEN').read(20500) a = request.urlopen('http://www.marketwatch.com/investing/currency/CUR_USDYEN').read(20500) b = a[19000:20500] idx_pricewrap = b.find('pricewrap') context = b[idx_pricewrap:idx_pricewrap+80] idx_bgLast = context.find('bgLast') rate = context[idx_bgLast+8:idx_bgLast+15] print rate print(rate) Also http://docs.python.org/library/2to3.html discusses the automated tool for converting python code. Greets Sander [1] http://docs.python.org/py3k/library/urllib.request.html#urllib.request.urlopen ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need help with converting script using 2.6's urllib2 to Python 3.1
On Mon, Oct 25, 2010 at 08:38, Sander Sweers sander.swe...@gmail.com wrote: Have you actually tried reading the documentation? Of course. Though I can see why you wondered. The _very_ first section of the urllib documentation we have urllib.request.urlopen [1]. Which looks to me what you are looking for as a replacement to urllib2.urlopen(). What I tried were these 3 lines: import urllib a = urllib.request.urlopen('http://www.marketwatch.com/investing/currency/CUR_USDYEN').read(20500) print(a[123:140]) which got me Traceback (most recent call last): File c:\P31Working\test_urllib.py, line 2, in module a = urllib.request.urlopen('http://www.marketwatch.com/investing/currency/CUR_USDYEN').read(20500) AttributeError: 'module' object has no attribute 'request' Process terminated with an exit code of 1 Doing it your way, from urllib import request a = request.urlopen('http://www.marketwatch.com/investing/currency/CUR_USDYEN').read(20500) print(a[123:140]) succeeds. Why? Anyway, thanks. Dick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need help with converting script using 2.6's urllib2 to Python 3.1
And trying 2to3.py, which I've used successfully before, gets C:\P26Working\Finished\For2to32to3 -w -f urllib dollar2yen_rate_simple.py Traceback (most recent call last): File C:\P26Working\Finished\For2to3\2to3.py, line 2, in module from lib2to3.main import main File C:\P26Working\Finished\For2to3\lib2to3\main.py, line 34 except os.error, err: ^ SyntaxError: invalid syntax dollar2yen_rate_simple.py can be seen at http://tutoree7.pastebin.com/4Aq9CuXS Also, Running this code in Python 3.1: from urllib import request a = request.urlopen('http://www.marketwatch.com/investing/currency/CUR_USDYEN').read(20500) b = a[19000:20500] idx_pricewrap = b.find('pricewrap') context = b[idx_pricewrap:idx_pricewrap+80] idx_bgLast = context.find('bgLast') rate = context[idx_bgLast+8:idx_bgLast+15] print(rate) gets Traceback (most recent call last): File c:\P31Working\test_urllib.py, line 4, in module idx_pricewrap = b.find('pricewrap') TypeError: expected an object with the buffer interface Process terminated with an exit code of 1 I have NO idea what that error means. Dick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need help with converting script using 2.6's urllib2 to Python 3.1
On 25 October 2010 18:19, Richard D. Moores rdmoo...@gmail.com wrote: Doing it your way, from urllib import request a = request.urlopen('http://www.marketwatch.com/investing/currency/CUR_USDYEN').read(20500) print(a[123:140]) succeeds. Why? Not sure how this exactly works but this is what I know. The difference is that urllib is now a directory with files in it while urllib2 was a single file. When you import module on a a single file module normally all functions and classes are available to you. Now that this is a directory some magic (or not?) has to happen in __init__.py (read I also don't know). But I am sure someone here will be able to explain this properly :-). Greets Sander ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Need help with converting script using 2.6's urllib2 to Python 3.1
So I finally find a relevant example in the docs: http://docs.python.org/py3k/library/urllib.request.html?highlight=urllib#examples The first example gave me some understanding and led me to revising my code to import urllib.request f = urllib.request.urlopen('http://www.marketwatch.com/investing/currency/CUR_USDYEN') a = f.read(20500).decode('utf-8') b = a[19000:20500] idx_pricewrap = b.find('pricewrap') context = b[idx_pricewrap:idx_pricewrap+80] idx_bgLast = context.find('bgLast') rate = context[idx_bgLast+8:idx_bgLast+15] print(rate) which works like a charm. Dick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor