Adding Characters to beginning and end of line that matches expression (Python)
<p>Background: I am tasked with taking content from one website (mostly HTML tables), and inserting it into a wiki type site. I am able to poll the content (it is dynamic) using a REST API and get HTML formatted output. I must take this output, and convert it into Wiki Markup in order to then insert it into the new site. I am having issues with the conversion part.</p>

<p>Currently I am using the html2text module in my test script as below:</p>

<pre><code>import os
import sys
import html2text

file = raw_input("File to convert: ")

h = html2text.HTML2Text()
with open(file, 'r') as f:
    dataContent = f.read()
    dataConverted = h.handle(dataContent)

with open('tempconvert', 'w') as f:
    f.write(dataConverted)
    #print dataConverted

# Add the | to the line beginnings and endings

with open('tempconvert', 'r') as f:
    tempContent = f.readlines()

with open('finalconvert', 'w') as f:
    for line in tempContent:
        if '|' in line:
            f.write('|' + line.rstrip('\n') + '| \n')
</code></pre>

<p>Now, the reason for all this input and output is because the html2text module doesn't insert a beginning or trailing "|" which the wiki syntax needs to recognize it as a table. </p>

<p>My questions are:</p>

<ol>
<li>This is super ugly ... any way to clean this up without writing to so many temporary files?</li>
<li>When I use the final search for adding the "|", it only writes the lines it acts upon (which makes sense given the arguments). How would I get it to act upon those lines, but also keep all the other lines that it doesn't act upon?</li>
<li>It would also be awesome for me to be able to take the first line in the table instance (you can tell this because of whitespace before and after the table) and append a "||" in order to signify its a header row. Is this also possible?</li>
</ol>

<p>1: You can eliminate the temporary files by using <a href="https://docs.python.org/2/library/stdtypes.html#str.split" rel="nofollow"><code>split</code></a> to convert from a single string to a list of strings. This has the side effect of removing the <code>\n</code> from each line, which you will have to add back or account for later.</p>

<pre><code>tempContent = dataConverted.split('\n')
</code></pre>

<p>2: There are two ways to fix this. First is to simply use an <code>else</code> to write the lines that you were skipping before. (No need for the <code>rstrip</code> if you use the <code>split</code> hint from above).</p>

<pre><code>if '|' in line:
    f.write('|' + line + '| \n')
else:
    f.write(line + '\n')
</code></pre>

<p>The other way is to update the line if it needs updating, then write it in either case.</p>

<pre><code>if '|' in line:
    line = '|' + line + '| '
f.write(line + '\n')
</code></pre>

<p>3: This is harder, because you don't just want to add those bars to <em>any</em> line that follows a blank line, you want to detect that there's a table coming up. This means you need some kind of look-ahead. Here's a little function you can use to see ahead automatically.</p>

<pre><code>def lookahead(seq):
    current = None
    for upcoming in seq:
        if current is not None:
            yield current, upcoming
        current = upcoming
    if current is not None:
        yield current, None
</code></pre>

<p>You'd use it like this:</p>

<pre><code>for line, upcoming in lookahead(tempContent):
    if (upcoming and '|' in upcoming) or ('|' in line):
        line = '|' + line + '|'
</code></pre>

python