Printing from a text file quirk

A

AWasilenko

I have a html document saved as a text file that I'm trying to load.
Its just the header section so my object is to read all the lines till
I hit the <title> tag, break, then read the rest. I have kinda
achieved what I wanted, but I'm getting a new line where I stopped.
It will become clear once you see the code. So my question is how do
I fix it, or is their an easier way?

Python code:
self.headertxt = open("pages/header.html","r")

*** Irrelevant code omitted ***

headerp1 = ""
for i in range(5):
headerp1 += self.headertxt.readline()
headerp2 = self.headertxt.readline(7)
headerp3 = self.headertxt.readline()
headerp4 = self.headertxt.read()
return headerp1 + headerp2 + headerp3 + pagetitle + headerp4

Here is the textfile im reading:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title></title>
<meta name="Generator" content="EditPlus" />
<meta name="Author" content="Adam W." />
<meta name="Description" content="Blah Blah Blah" />
</head>"""

and here is the output:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>
this is a new title
</title>
<meta name="Generator" content="EditPlus" />
<meta name="Author" content="Adam W." />
<meta name="Description" content="Blah Blah Blah" />
</head>"""

As you can see what I was trying to acheive was <title>the title</
title> all on the same line. Help!
 
A

AWasilenko

Humm I think I messed up the code before I pasted it, I am now able to
get the left tag on the same line with this:

headerp1 = ""
for i in range(5):
headerp1 += self.headertxt.readline()
headerp2 = self.headertxt.readline(7)
headerp3 = self.headertxt.readline()
headerp4 = self.headertxt.read()
return headerp1 + headerp2 + pagetitle + headerp3 + headerp4

output:
<title>this is a new title
</title>

Now all's left is the </title> tag to get on the same line, or a
better solution, this seems like a very awkward way to do what I want.
 
7

7stud

self.headertxt = open("pages/header.html","r")

*** Irrelevant code omitted ***

headerp1 = ""
for i in range(5):
headerp1 += self.headertxt.readline()
headerp2 = self.headertxt.readline(7)
headerp3 = self.headertxt.readline()
headerp4 = self.headertxt.read()
return headerp1 + headerp2 + headerp3 + pagetitle + headerp4

Here is the textfile im reading:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title></title>
<meta name="Generator" content="EditPlus" />
<meta name="Author" content="Adam W." />
<meta name="Description" content="Blah Blah Blah" />
</head>"""

and here is the output:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>
this is a new title
</title>
<meta name="Generator" content="EditPlus" />
<meta name="Author" content="Adam W." />
<meta name="Description" content="Blah Blah Blah" />
</head>"""

As you can see what I was trying to acheive was <title>the title</
title> all on the same line. Help!

Where can I see that? From the python language documentation here:

http://docs.python.org/lib/lib.html

you can look up readline() and see:
 
A

AWasilenko

Where can I see that? From the python language documentation here:
http://docs.python.org/lib/lib.html

you can look up readline() and see:
----------
readline( [size])
Read one entire line from the file. A trailing newline character is
kept in the string
---------

There is a function called strip() that will strip off all leading and
trailing whitespace on a string, e.g. a newline.- Hide quoted text -

- Show quoted text -

Thank you! Just what I needed :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top