Checking each item in m.group()?

Matimus mccredie at gmail.com
Mon Jun 2 15:06:21 EDT 2008


On Jun 2, 11:42 am, "nos... at nospam.com" <Gilles@> wrote:
> Hello
>
> I need to go through each line of a CSV file, and extract some fields
> using a regex. Then, I need to check each retrieved field, and if it
> looks like "", turn this into NULL so that it's correct SQL.
>
> I tried a few things, but still can't it working:
> ========
> #Second field might be empty -> ""
> #"Col1",""
> #"Col1","Col2"
> p = re.compile('^"(.+?)","(.*?)"$')
>
> for line in textlines:
>         m = p.search(line)
>         if m:
>                 #Check each column : if '', then turn into NULL
>
>                 """
>                 for col in line:
>                         if col == "":
>                                 col = "NULL"
>                 """
>
>                 """
>                 for col in m.group():
>                         if col == "":
>                                 col="NULL"
>                 """
>
>                 """
>                 for col in m.group(0):
>                         if col == "":
>                                 col="NULL"
>                 """
>
>                 """
>                 for i in range (0,len(line)):
>                         if line[i] == "":
>                                 line[i]="NULL"
>                 """
>
>                 """
>                 for i in range(1,len(m.group(0))):
>                         if m.group(i) == "":
>                                 m.group(i)="NULL"
>                 """
>
>                 sql = "INSERT INTO mytable (col1, col2) VALUES
> ('%s','%s')" % (m.group(1),m.group(2))
>                 print sql
> f.close()
> ========
>
> Does someone know the correct syntax?
>
> Thank you.

I think you want to use 'groups' instead of 'group'.

Here is a brief example. Note that this code is very insecure and
susceptible to a SQL injection attack. Hopefully these csv files are
from a trusted source.

sql = "INSERT INTO mytable (col1, col2) VALUES ('%s','%s')"%tuple(
        (c, "NULL")[c == ''] for c in m.groups()
        )

Also, check out the csv module for parsing your csv file.

Matt



More information about the Python-list mailing list