3 Replies - 1213 Views - Last Post: 01 December 2009 - 04:32 AM Rate Topic: -----

#1 truekid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 28-November 09

need help with queues and stacks

Post icon  Posted 28 November 2009 - 06:25 PM

I hope you all had a good turkey day. Well I had a good one to until i realized i had a hw due tom. well its fairly simple, We are supposed to create a progrom in python that takes a html file and checks to see if the tags are ballanced using stacks and queues with out importing any thing. . I am stuck on a simple part. here is my code so far....

def main():

	if len(sys.argv) != 2:
		print "This program requires command line arguments."
		print "The first argument is the filename of the input file."
		print "The second argument is the filename of the output file."   
		sys.exit()
	
	#here i am opening the html file name that contains the tags and text. 
	filename = open(sys.argv[1], "r")

	# since i cant import anything i will using pythons list types to do this
	tags = []
	for line in filename:
			# what do i put here to read in the tags? thats all i need so far?
			print line

	filename.close()
main()




The help i need is dealing with reading the tags? how would one do so? I dont know if i am breaking any rules, but thats that code i have so far. Thank you for helping a complete newb.

Is This A Good Question/Topic? 0
  • +

Replies To: need help with queues and stacks

#2 sempron644  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 23-November 09

Re: need help with queues and stacks

Posted 28 November 2009 - 07:22 PM

View Posttruekid, on 28 Nov, 2009 - 05:25 PM, said:

I hope you all had a good turkey day. Well I had a good one to until i realized i had a hw due tom. well its fairly simple, We are supposed to create a progrom in python that takes a html file and checks to see if the tags are ballanced using stacks and queues with out importing any thing. . I am stuck on a simple part. here is my code so far....

def main():

	if len(sys.argv) != 2:
		print "This program requires command line arguments."
		print "The first argument is the filename of the input file."
		print "The second argument is the filename of the output file."   
		sys.exit()
	
	#here i am opening the html file name that contains the tags and text. 
	filename = open(sys.argv[1], "r")

	# since i cant import anything i will using pythons list types to do this
	tags = []
	for line in filename:
			# what do i put here to read in the tags? thats all i need so far?
			print line

	filename.close()
main()




The help i need is dealing with reading the tags? how would one do so? I dont know if i am breaking any rules, but thats that code i have so far. Thank you for helping a complete newb.



Hello,
You have to think about iteration methods. You might need a list, or a container that can hold your string from the file. Once you have the file open, assign it a variable and work with it from there, depending on what iteration or string methods you decide to utilize. You can also think about the iter builtin, but I'm not sure if you need that here.

I could probably figure out whats needed with more time, but as far as reading html code, I've done a limited amount of work with that. There is a module for these types of things, can't think of the name off hand.
Was This Post Helpful? 0
  • +
  • -

#3 truekid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 28-November 09

Re: need help with queues and stacks

Posted 29 November 2009 - 01:18 PM

Ok experimenting a little and using module re

i came up with this. now this works for sum tags but not all of them here is the code
import sys
import string
import re
def main():

	if len(sys.argv) != 2:
		print "This program requires command line arguments."
		print "The first argument is the filename of the input file."
		print "The second argument is the filename of the output file."   
		sys.exit()

	file = open(sys.argv[1], 'r')
	text = file.readlines()
	file.close()

	tags = []
	p = re.compile(r'<.*?>' or '<.*?/>')
	for line in text: 
		if p.search (line):
			print line,
 
	

main()



THIS IS THE INPUT
<html>
<head>
<title> xhtml data file </title>
</head>

<body>
<center><h1>
This is a level 1 heading that's centered
</h1></center>

<b>emphasized text</b>
<P>This is a paragraph</P>
<P>
<br />
we're skipping some lines using a self-closing tag
<br />
<br />
before ending the second paragraph</P>
the next tag is a horizontal-rule which is also self-closing
<hr />
<P>the third paragraph is started
<foo>this tag foo is not an html tag but could be an xml tag</foo></P>
<P>
the fourth paragraph
<br />
<tag1>
skips some lines and opens 3 tags
<br />
<tag2>
<br />
<tag3>
<br />
before it starts to close them
</tag3>
<br />
</tag2>
<br />
</tag1>
<br />
they're all closed now</P>
<P>it ends with the fifth paragraph</P>
</body>
</html>
and finally closes the body and the document


THIS IS THE OUTPUT
Your greeting/instructions go here!
<html>
<head>
<title>
</title>
</head>
<body>
<center>
<h1>
</h1>
</center>
<b>
</b>
<P>
</P>
<P>
<br />
<br />
<br />
</P>
<hr />
<P>
<foo>
</foo>
</P>
<P>
<br />
<tag1>
<br />
<tag2>
<br />
<tag3>
<br />
</tag3>
<br />
</tag2>
<br />
</tag1>
<br />
</P>
<P>
</P>
</body>
</html>
Phase 1: End of file was reached for xhtml.dat with no errors

<title> matches </title>
<head> matches </head>
<h1> matches </h1>
<center> matches </center>
<b> matches </b>
<P> matches </P>
<br /> is self-closing
<br /> is self-closing
<br /> is self-closing
<P> matches </P>
<hr /> is self-closing
<foo> matches </foo>
<P> matches </P>
<br /> is self-closing
<br /> is self-closing
<br /> is self-closing
<br /> is self-closing
<tag3> matches </tag3>
<br /> is self-closing
<tag2> matches </tag2>
<br /> is self-closing
<tag1> matches </tag1>
<br /> is self-closing
<P> matches </P>
<P> matches </P>
<body> matches </body>
<html> matches </html>
Phase 2: The tags match in this document.

any help as to how to get it just print the tags and store them into a list???

This post has been edited by truekid: 29 November 2009 - 01:17 PM

Was This Post Helpful? 0
  • +
  • -

#4 chemicalfan  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 88
  • Joined: 16-October 09

Re: need help with queues and stacks

Posted 01 December 2009 - 04:32 AM

Instead of printing the line, couldn't you append them to a list with the append method?
To get just the tags, you might need to use a 2nd re, to just select that specific text (i.e. the tag)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1