[Tutor] Generating/Parsing XML

2011-04-12 Thread tee chwee liong

hi, 
 
i'm a bit stuck here. i have a code dict1.py that writes to an xml and another 
script parsedict1.py to parse the content of the xml. 
when i write it to the xml i wrote it as dictionary but since it can't accept 
dict format i convert it to string. 
below is output  of the parsedict1.py when i print it out. 
 
(None, '{1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6}')
(None, '{1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12}')
 
Questions:

Why it shows as None and not LINK or Lanestat?
How can i modify the code to show LINK or Lanestat? 
How can i extract the key in the dictionary since there are 2 elements only. 
for eg: if i print a[1], it will show {1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6}. 
i want to be able to extract info like this: Link [1] = 1, just like how it 
is done in dictionary. 
 
pls help to advise.
 
thanks
tcl
p/s: attached code for dict1.py, parsedict1.py, dict1.xml
 
dict1.py code:
import elementtree.ElementTree as ET
def port_status():
a=1
b=2
c=3
d=4
e=5
f=6
return (a,b,c,d,e,f)
call_port=port_status()
link_status={1:call_port[0], 2:call_port[1], 3:call_port[2], 4:call_port[3], 
5:call_port[4], 6:call_port[5]}
lane_status={1:call_port[0]+1, 2:call_port[1]+2, 3:call_port[2]+3, 
4:call_port[3]+4, 5:call_port[4]+5, 6:call_port[5]+6}
print link_status
print lane_status
###write to xml
root = ET.Element(Test)
head1 = ET.SubElement(root, Default_Config)
title = ET.SubElement(head1, LINK)
title.text = str(link_status)
title = ET.SubElement(head1, Lanestat)
title.text = str(lane_status)
tree = ET.ElementTree(root)
tree.write(C:\\Python25\\myscript\\cmm\\dict1.xml)
 
parsedict1.py code:
import elementtree.ElementTree as ET
tree = ET.parse(dict1.xml)
doc = tree.getroot()

for elem in doc.findall('Default_Config/LINK'):
#print elem.get('LINK'), elem.text
a=elem.get('LINK'), elem.text
print a
for elem in doc.findall('Default_Config/Lanestat'):

#print elem.get('LINK'), elem.text
a=elem.get('LINK'), elem.text
print a   import elementtree.ElementTree as ET

def port_status():
a=1
b=2
c=3
d=4
e=5
f=6
return (a,b,c,d,e,f)

call_port=port_status()

link_status={1:call_port[0], 2:call_port[1], 3:call_port[2], 4:call_port[3], 
5:call_port[4], 6:call_port[5]}
lane_status={1:call_port[0]+1, 2:call_port[1]+2, 3:call_port[2]+3, 
4:call_port[3]+4, 5:call_port[4]+5, 6:call_port[5]+6}

print link_status
print lane_status

###write to xml
root = ET.Element(Test)
head1 = ET.SubElement(root, Default_Config)
title = ET.SubElement(head1, LINK)
title.text = str(link_status)

title = ET.SubElement(head1, Lanestat)
title.text = str(lane_status)

tree = ET.ElementTree(root)
tree.write(C:\\Python25\\myscript\\cmm\\dict1.xml)
TestDefault_ConfigLINK{1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6}/LINKLanestat{1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12}/Lanestat/Default_Config/Test
This script works in parsing config file: dict1.xml


#import sys
import elementtree.ElementTree as ET

tree = ET.parse(dict1.xml)
doc = tree.getroot()


for elem in doc.findall('Default_Config/LINK'):
#print elem.get('LINK'), elem.text
a=elem.get('LINK'), elem.text
print a

for elem in doc.findall('Default_Config/Lanestat'):

#print elem.get('LINK'), elem.text
a=elem.get('LINK'), elem.text
print a

#sys.exit()
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Import multiple lines of text into a variable

2011-04-12 Thread Alan Gauld


Sean Carolan scaro...@gmail.com wrote


 The first two lines are redundant you only need the last one.

I should have clarified, the if line.startswith part was used to
break out of the previous for loop, which was used to import the
other, shorter strings.


Thats fair enough if you are doing something with those 
shorter strings. But if not the whole loop is redundant, 
you only need the split. Your original post did not mention 
any processing of the earlier lines.


But even if you were you could still use the split() first 
then process the first element in a loop and assign the 
second element to your variable:


stuff, store = theFile.read().split('notes\n')
for line in stuff.split():
  # process thing

That way there is no need for a break test.

HTH.,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Converting files

2011-04-12 Thread sunil tech
thank you for all your valuable suggestions...
but i want it to be converted using python code ..

On Tue, Apr 12, 2011 at 2:38 AM, Evans Anyokwu onyx...@gmail.com wrote:

 I use Openoffice and it has an option to export your files to .pdf and lots
 of other file formats.
 It's a free download - and has all the usual Office applications...

 Search for 'OpenOffice' online.


 On Mon, Apr 11, 2011 at 9:48 PM, Alan Gauld alan.ga...@btinternet.comwrote:


 sunil tech sunil.tech...@gmail.com wrote


  is there any way to convert any file (eg: document files  image files)
 to
 .pdf?

 if so, kindly share...


 Install a PDF print driver and then print the file to that printer.
 Set it to save as a file. Then if its printable you can get it as a PDF.

 You can do the same with postscript(and postscript drivers
 come with most OS). Then send the postscript file to Adobe's
 web site to get them to generate the PDF from postscript.
 (You can also download free convertors)

 Finally, and because this is a Python list,  you could use a
 Python library and generate the file yourself - but while thats
 ok for your own data its a lot harder for many file types!

 HTH,

 --
 Alan Gauld
 Author of the Learn to Program web site
 http://www.alan-g.me.uk/



 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor



 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Converting files

2011-04-12 Thread Andrés Chandía


I don't know how to do it, but I'm 99% sure I have seen a something related in 
cherrypy pages,
try googling cherrypy pdf generation or something similiar

Good
luck!

El Mar, 12 de Abril de 2011, 10:02, sunil tech escribió:
  thank you for all your valuable suggestions...
but i want it to be
converted using python code ..
  

On Tue, Apr 12, 2011 at 2:38 AM, Evans Anyokwu onyx...@gmail.com wrote:
  I
use Openoffice and it has an option to export your files to .pdf and lots of 
other file
formats.
It's a free download - and has all the usual Office applications...

Search for 'OpenOffice' online.

 

  

On Mon, Apr 11, 2011 at 9:48 PM, Alan Gauld alan.ga...@btinternet.com wrote:

   
 sunil tech sunil.tech...@gmail.com wrote

 
  is there any way to convert any
file (eg: document files  image files) to
 .pdf?
 
 if so, kindly
share...
  

Install a PDF print driver and then print the file to that printer.
 Set it to save as a
file. Then if its printable you can get it as a PDF.
 
 You can do the same with
postscript(and postscript drivers
 come with most OS). Then send the postscript file to
Adobe's
 web site to get them to generate the PDF from postscript.
 (You can also
download free convertors)
 
 Finally, and because this is a Python list,  you
could use a
 Python library and generate the file yourself - but while thats
 ok for
your own data its a lot harder for many file types!
 
 HTH,
 
 -- 
 Alan Gauld
 Author of the Learn to Program web site
 http://www.alan-g.me.uk/

 

 
 
 ___
 Tutor maillist
 -  Tutor@python.org
 To unsubscribe or change
subscription options:
 http://mail.python.org/mailman/listinfo/tutor
 



 


___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change
subscription options:
 http://mail.python.org/mailman/listinfo/tutor
 



 


___
andrés
chandía

P No imprima
innecesariamente. ¡Cuide el medio ambiente!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Converting files

2011-04-12 Thread Andrés Chandía


this is the list for cherrypy

cherrypy-users
cherrypy-us...@googlegroups.com


El Mar, 12 de Abril de 2011, 10:02,
sunil tech escribió:
  thank you for all your valuable
suggestions...
but i want it to be
converted using python code ..
  

On Tue, Apr 12, 2011 at 2:38 AM, Evans Anyokwu onyx...@gmail.com wrote:
  I
use Openoffice and it has an option to export your files to .pdf and lots of 
other file
formats.
It's a free download - and has all the usual Office applications...

Search for 'OpenOffice' online.

 

  

On Mon, Apr 11, 2011 at 9:48 PM, Alan Gauld alan.ga...@btinternet.com wrote:

   
 sunil tech sunil.tech...@gmail.com wrote

 
  is there any way to convert any
file (eg: document files  image files) to
 .pdf?
 
 if so, kindly
share...
  

Install a PDF print driver and then print the file to that printer.
 Set it to save as a
file. Then if its printable you can get it as a PDF.
 
 You can do the same with
postscript(and postscript drivers
 come with most OS). Then send the postscript file to
Adobe's
 web site to get them to generate the PDF from postscript.
 (You can also
download free convertors)
 
 Finally, and because this is a Python list,  you
could use a
 Python library and generate the file yourself - but while thats
 ok for
your own data its a lot harder for many file types!
 
 HTH,
 
 -- 
 Alan Gauld
 Author of the Learn to Program web site
 http://www.alan-g.me.uk/

 

 
 
 ___
 Tutor maillist
 -  Tutor@python.org
 To unsubscribe or change
subscription options:
 http://mail.python.org/mailman/listinfo/tutor
 



 


___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change
subscription options:
 http://mail.python.org/mailman/listinfo/tutor
 



 


___
andrés
chandía

P No imprima
innecesariamente. ¡Cuide el medio ambiente!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Converting files

2011-04-12 Thread Steven D'Aprano

sunil tech wrote:

Hi all...

is there any way to convert any file (eg: document files  image files) to
.pdf?


If the file is printable, print it to a PDF gateway that generates a PS 
or PDF file.


If it is a Microsoft Office document, or similar, then install 
OpenOffice or LibreOffice and use it to convert to PDF.


If it's not printable, then your question doesn't make sense. What would 
an ISO or EXE or ZIP file look like converted to PDF?


But I wonder whether you bothered to google for python pdf first? The 
very first link is a comprehensive review of installing and using a PDF 
generator from Python.





--
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Import multiple lines of text into a variable

2011-04-12 Thread Steven D'Aprano

Sean Carolan wrote:

if line.startswith('notes'):
  break
notes = open('myfile','r').read().split(notes:\n')[1]

The first two lines are redundant you only need the last one.


I should have clarified, the if line.startswith part was used to
break out of the previous for loop, which was used to import the
other, shorter strings.



Just for reference, import has special meaning in Python, and you hurt 
my brain by using it as a synonym for read.


For what it's worth, here's my solution. Rather than use the funky new 
open files are iterable feature, go back to the old-style way of 
reading line by line:


# untested
fp = open(myfile.txt)
for while True:
line = fp.readline()  # read one line
if not line:
# nothing left to read
break
if ham in line:
process_ham(line)  # Mmmm, processed ham...
if spam in line:
process_spam(line)
if line.startswith(notes):
notes = fp.read()  # reads the rest of the file
fp.close()


Note that it is okay to mix calls to read() and readline(), but it is 
NOT okay to mix iteration over a file with calls to read() or readline().





--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Print images links

2011-04-12 Thread l . leichtnam
Hello,

I would like to print the adresses of every image that are on the main page of 
www.columbia.edu. Do you know how to do this?

Thanks
Sent from my BlackBerry® on the MetroPCS Network
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Retrieve data

2011-04-12 Thread l . leichtnam
Hello everyone,

I would to retrieve data, and especially the temperature and the weather from 
http://www.nytimes.com/weather. And I don't know how to do so.

Thank you,

Louis
Sent from my BlackBerry® on the MetroPCS Network
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Print images links

2011-04-12 Thread Noah Hall
On Tue, Apr 12, 2011 at 5:40 PM,  l.leicht...@gmail.com wrote:
 Hello,

 I would like to print the adresses of every image that are on the main page 
 of www.columbia.edu. Do you know how to do this?


Yes, I do. ;)

As this sounds like homework -
Firstly, you want to grab the source code of the webpage.
Secondly, you want to find what defines each image link - I'll give
you a clue, think of HTML markup for images.
Thirdly, use a regex to extract the link and either print it directly,
or put it into something you can reuse later.

Hope this helps.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Print images links

2011-04-12 Thread Prasad, Ramit
I would use urllib2 with Python (for 2.x) plus some regular expressions.


Ramit



Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423


-Original Message-
From: tutor-bounces+ramit.prasad=jpmchase@python.org 
[mailto:tutor-bounces+ramit.prasad=jpmchase@python.org] On Behalf Of 
l.leicht...@gmail.com
Sent: Tuesday, April 12, 2011 11:41 AM
To: tutor@python.org; tutor-boun...@python.org
Subject: [Tutor] Print images links

Hello,

I would like to print the adresses of every image that are on the main page of 
www.columbia.edu. Do you know how to do this?

Thanks
Sent from my BlackBerry(r) on the MetroPCS Network
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
This communication is for informational purposes only. It is not
intended as an offer or solicitation for the purchase or sale of
any financial instrument or as an official confirmation of any
transaction. All market prices, data and other information are not
warranted as to completeness or accuracy and are subject to change
without notice. Any comments or statements made herein do not
necessarily reflect those of JPMorgan Chase  Co., its subsidiaries
and affiliates.

This transmission may contain information that is privileged,
confidential, legally privileged, and/or exempt from disclosure
under applicable law. If you are not the intended recipient, you
are hereby notified that any disclosure, copying, distribution, or
use of the information contained herein (including any reliance
thereon) is STRICTLY PROHIBITED. Although this transmission and any
attachments are believed to be free of any virus or other defect
that might affect any computer system into which it is received and
opened, it is the responsibility of the recipient to ensure that it
is virus free and no responsibility is accepted by JPMorgan Chase 
Co., its subsidiaries and affiliates, as applicable, for any loss
or damage arising in any way from its use. If you received this
transmission in error, please immediately contact the sender and
destroy the material in its entirety, whether in electronic or hard
copy format. Thank you.

Please refer to http://www.jpmorgan.com/pages/disclosures for
disclosures relating to European legal entities.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Retrieve data

2011-04-12 Thread Rafael Durán Castañeda
You cand use urllib to connect that web page and then use a custom html
parser to get exactly what you want, even I think you could use regex; just
look source code from the webpage, check for the data you are interested in
and get it.


2011/4/12 l.leicht...@gmail.com

 Hello everyone,

 I would to retrieve data, and especially the temperature and the weather
 from http://www.nytimes.com/weather. And I don't know how to do so.

 Thank you,

 Louis
 Sent from my BlackBerry® on the MetroPCS Network
 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Retrieve data

2011-04-12 Thread James Reynolds
NYtimes has an API http://developer.nytimes.com/

http://developer.nytimes.com/No clue on how well it works or even if the
weather is in the API. Looks like a lot of fluff, like comments and such.

At any rate, maybe you can request they add weather to their API.

On Tue, Apr 12, 2011 at 12:36 PM, l.leicht...@gmail.com wrote:

 Hello everyone,

 I would to retrieve data, and especially the temperature and the weather
 from http://www.nytimes.com/weather. And I don't know how to do so.

 Thank you,

 Louis
 Sent from my BlackBerry® on the MetroPCS Network
 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription options:
 http://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Print images links

2011-04-12 Thread Steven D'Aprano

l.leicht...@gmail.com wrote:

Hello,

I would like to print the adresses of every image that are on the main page of 
www.columbia.edu. Do you know how to do this?



Pretty much the same answer applies as for your previous question.



--
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Retrieve data

2011-04-12 Thread Steven D'Aprano

l.leicht...@gmail.com wrote:

Hello everyone,

I would to retrieve data, and especially the temperature and the weather from 
http://www.nytimes.com/weather. And I don't know how to do so.


Consider whether the NY Times terms and conditions permit such automated 
scraping of their web site.


Be careful you do not abuse their hospitality by hammering their web 
site unnecessarily (say, by checking the weather eighty times a minute).


Consider whether they have a public API for downloading data directly. 
If so, use that. Otherwise:


Use the urlib2 and urlib modules to download the raw HTML source of the 
page you are interested in. You may need to use them to login, to set 
cookies, set the referer [sic], submit data via forms, change the 
user-agent... it's a PITA. Better to use an API if the web site offers one.


Use the htmllib module to parse the source looking for the information 
you are after. If their HTML is crap, as it so often is with commercial 
websites that should know better, download and install BeautifulSoup, 
and use that for parsing the HTML.


Don't be tempted to use regexes for parsing the HTML. That is the wrong 
solution. Regexes *seem* like a good idea for parsing HTML, and for 
simple tasks they are quick to program, but they invariably end up being 
ten times as much work as a proper HTML parser.


If the content you are after requires Javascript, you're probably out of 
luck.



--
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor