[Tutor] Breaking string at commas

Remco Gerlich scarblac@pino.selwerd.nl
Mon, 12 Mar 2001 19:01:15 +0100


On Mon, Mar 12, 2001 at 12:29:35PM -0500, Shawhan, Douglas (GEAE, GECC) wrote:
> I am trying to write a script that will take comma seperated text from a
> file, break the strings into seperate units at the linefeeds then seperate
> the comma seperated text into strings, insert html formatting then output
> the whole shebang into an html file. I have figured out how to read the info
> from the flat files and squirt them to html with formatting (do what you
> can, if it's all that you can! :-).
> 
> ---------begin ugly script---------------
> f=open('/windows/desktop/shuffles/hippie.db','r')
> grunty=f.read()
> f=open('/windows/desktop/shuffles/whofrom.db','r')
> whofrom=f.read()
> f=open('/windows/desktop/shuffles/whofrom.db','r')
> whoto=f.read()
> f.write('<html><h1><b>MOVES/INSTALLS</b></h1><body bgcolor=white><font
> face=\"arial,helvetica\" size=\"2\"><table width=580><tr><td
> bgcolor=black>&nbsp;&nbsp;</td><tr><td>'+`grunty`+'</td><tr><td
> bgcolor=black>&nbsp;&nbsp;</td><tr><td>'+`whofrom`+'</td><tr><td
> bgcolor=black>&nbsp;&nbsp;</td><tr><td>'+`whoto`+'</td></table></font></html
> >')
> f.close()
> ----------end ugly script--------------

I suppose that the whole 'f.write(' thing was on a single line, that's ugly
indeed :). With """triple quoted strings""" you can span multiple lines.

> Now the tricky part for me is: How can I first bust the files apart at the
> line breaks,

You can split it on "\n", or read them in as a list of lines in the first
place using f.readlines(). However, readlines() leaves the \n in the
strings, and since you don't want that here, it's easiest to split it
yourself, using string.split (it's also a string method in newer versions,
but this probably has to run on some web server, they often have 1.5.2, so
I'm assuming that).

> then search for the unique id in the resulting output? Tuples?

I don't know what this id is, but you can split the comma seperated line
with a further string.split on ",", as long as it's not too complicated (if
the data itself can have a comma inside it you'll have some escaping thing,
simply splitting will be wrong then).
 
> The other trick is: How am I going to bust apart those strings within the
> chosen output to insert the formatting?

For the formatting, you want to make a template of the html, with things
like '%(howto)s' inside it, then you can fill in the template with the %
operator.


I'd write it like this:

----------------------------------------------------------------

html_template = """
<html>
  <h1>
    <b>MOVES/INSTALLS</b>
  </h1>
<body bgcolor=white>
  <font face="arial,helvetica" size="2">
  <table width=580>
    <tr>
      <td bgcolor=black>&nbsp;&nbsp;</td>
    <tr>
      <td>%(grunty)s</td>
    <tr>
      <td bgcolor=black>&nbsp;&nbsp;</td>
    <tr>
       <td>%(whofrom)s</td>
    <tr>
       <td bgcolor=black>&nbsp;&nbsp;</td>
    <tr>
       <td>%(whoto)s</td>
  </table>
  </font>
</html>
"""

# (You can't have a <h1> before <body>, but it's the example you use...)
# You also didn't close your <tr> tags!
# Note I didn't have to escape " in a """ string
# Instead of writing a template you may want to use HTMLGen.

# Now the files stuff:

import string # For the split function

information = {} # Dict in which we store things for the template

# You also should have some error detection here...
grunty = open('/windows/desktop/shuffles/hippie.db','r').read()
grunty = string.split(grunty, "\n") # Divide into lines
# As an example, we'll print the split version of the first line of the file,
# I don't know what you need
information['grunty'] = repr(string.split(grunty[0], ","))

whofrom = open('/windows/desktop/shuffles/whofrom.db','r').read()
whofrom = string.split(whofrom, "\n")
information['whofrom'] = repr(string.split(whofrom[0], ","))

whoto = open('/windows/desktop/shuffles/whofrom.db','r').read()
whoto = string.split(whoto, "\n")
information['whofrom'] = repr(string.split(whofrom[0], ","))

# Now we fill in the template using the dictionary
HTML = html_template % information

# And write it
f=open('punker.html','w')
f.write(HTML)
f.close()

----------------------------------------------------------------

-- 
Remco Gerlich