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","Col 2"
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.g roup(0))):
if m.group(i) == "":
m.group(i)="NUL L"
"""
sql = "INSERT INTO mytable (col1, col2) VALUES
('%s','%s')" % (m.group(1),m.g roup(2))
print sql
f.close()
========
Does someone know the correct syntax?
Thank you.
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","Col 2"
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.g roup(0))):
if m.group(i) == "":
m.group(i)="NUL L"
"""
sql = "INSERT INTO mytable (col1, col2) VALUES
('%s','%s')" % (m.group(1),m.g roup(2))
print sql
f.close()
========
Does someone know the correct syntax?
Thank you.
Comment