Can Python or SQLite search and replace output on the fly?
<p>Programs:  SQLite database 3.14.1 &amp; Python 2.7</p>

<p>I have a table called transactions.  The field "name" has names in it.  I can use Python to return the rows from selected fields to a text file based on criteria specified from tranType while adding some strings to it on the fly:</p>

<pre><code>e = open("export.txt", "w+")

sqlF1 = """SELECT name, quantity, item, tDate, tranType FROM transactions WHERE tranType LIKE "%sell%"

"""

c.execute(sqlF1)
for row in c.execute(sqlF1):
    e.write('%s bought QTY %s of %s from %s on date %s\n' % row)

e.close()
</code></pre>

<p>This is fine and good.  In the name field, sometimes there are first and last names and sometimes it's just the first name.  </p>

<p>What I want to do is, while getting each row, check the name - if there is a space in the field (like a first last entry), change the space to a "+" symbol on the fly.  I don't want to change the field beforehand and replace all the spaces with pluses.</p>

<p>Can this be done?</p>

<p>What I know/don't know:</p>

<p>Based on my limited knowledge and experience, I can't figure out how to do it. I was thinking the only way I can make this happen is to dump the names into another field and change all the spaces to pluses and then redo the SQL part to grab the different field.  </p>

<p>Any advice is greatly appreciated.  I'm taking some Python courses soon (and doing some studying on SQLite too, so hopefully I won't be continuously inundating StackOverflow with too many dumb questions.</p>

<p>Thanks!</p>

<p>With the <a href="http://www.sqlite.org/lang_corefunc.html#replace" rel="nofollow">replace() function</a>, this can be done on the fly:</p>

<pre class="lang-sql prettyprint-override"><code>SELECT replace(name, ' ', '+'), quantity, item, tDate FROM ...
</code></pre>

python sqlite loops replace string-concatenation