Checking each item in m.group()?
Gilles Ganault
nospam at nospam.com
Sun Jun 15 19:48:59 EDT 2008
On Mon, 02 Jun 2008 17:49:11 -0700, Scott David Daniels
<Scott.Daniels at Acm.Org> wrote:
>(c, "NULL") is a tuple; it is being indexed by the boolean "c == ''"
>Since False is 0, and True is 1, the expression picks out "NULL"
>exactly when c is the zero-length string.
Thanks Scott, and also to Peter above, and sorry for not having
answered earlier.
I'm having two problems: The first code doesn't strip the
double-quotes when the string is empty. IOW, "not empty" or NULL, but
not "NULL"
The second code hits "IndexError: list index out of range" because of
Yield() which I don't seem to be using correctly:
==========
p = re.compile("^(\d+)\t(.*?)")
for line in textlines:
m = p.search(line)
if m:
sql = sql + 'INSERT INTO mytable (col1,col2) VALUES
("%s","%s");' % tuple ((c,"NULL")[c == ''] for c in m.groups())
#cursor.execute(sql)
connection.close(True)
print sql
==========
import csv
import sqlite3 as sqlite
def records(infile):
for row in csv.reader(infile):
#IndexError: list index out of range
#BAD yield row[0], row[1] or None
#BAD yield row[0] or None, row[1] or None
def main():
db = sqlite.connect("test.sqlite")
cursor = db.cursor()
#How to handle empty columns, ie. <TAB><TAB>?
cursor.executemany("insert into mytable (col1,col2) values (?,?);",
records("test.sqlite"))
if __name__ == "__main__":
main()
==========
Thank you.
More information about the Python-list
mailing list