Parsing a website with Python 2.5.1 and 3-1.2

With Django sub forum.
KBleivik
Site Admin
Posts: 178
Joined: Tue Sep 29, 2009 6:25 pm
Location: Moss Norway
Contact:

Parsing a website with Python 2.5.1 and 3-1.2

Postby KBleivik » Mon Jan 31, 2011 6:55 pm

There is a difference between Python 2.5.1 and Python 3-1.3 code. For more information read here:

http://wiki.python.org/moin/Python2orPython3

http://docs.python.org/release/3.1.2/whatsnew/3.0.html

The following code shows a difference between parsing a page http://primes.utm.edu/lists/small/10000.txt on the internet in Python 2.5.1 and Python 3-1.2 (my installed verions). The python 2.5.1 code is commented out.

# Prim.py
#
# Kjell Bleivik 2010: http://www.kjellbleivik.com - http://www.oopschool.com - http://www.digitalpunkt.no,
#
# ---------------------------------------------------------------------------------------------------
#
# Opens the webdocument with The First 10,000 Primes, read the file and print the
# content. For version 3.1.2. Version 2.5.1 is commented out.
# Reference: http://diveintopython3.org/porting-code ... -2to3.html
#
# ---------------------------------------------------------------------------------------------------
#
#import urllib
#file = urllib.urlopen('http://primes.utm.edu/lists/small/10000.txt')
#primes = file.read()
#print (primes)

import urllib.request
file = urllib.request.urlopen('http://primes.utm.edu/lists/small/10000.txt')
primes = file.read().decode("utf8")

my_primes = []
for i,line in enumerate(primes.split('\n')):
if i > 3 and i < 1004:
temp = ((int(item) for item in line.split()))
for i in temp:
my_primes.append(i)
print (my_primes)
#You can take out the numbers you want.
#10 first prime number
#print (my_primes[0:10])

Return to “Python”

Who is online

Users browsing this forum: No registered users and 1 guest