scanning through page and replacing all instances of 00:00:00.00

K

Kun

I have a python-cgi file that pulls data from an sql database, i am
wondering what is the easiest way to remove all instances of
'00:00:00.00' in my date column.

how would i write a python script to scan the entire page and delete all
instances of '00:00:00.00', would i use regular expressions?
 
T

Tim Chase

I have a python-cgi file that pulls data from an sql
database, i am wondering what is the easiest way to
remove all instances of '00:00:00.00' in my date column.

how would i write a python script to scan the entire page
and delete all instances of '00:00:00.00', would i use
regular expressions?

No need for a regexp:

someString = someString.replace("00:00:00.00", "")

I'd recommend doing it before you print the values out,
rather than try and "scan the entire page" after you've
printed them.

Easy 'nuff.

-tkc
 
F

Fredrik Lundh

Kun said:
I have a python-cgi file that pulls data from an sql database, i am
wondering what is the easiest way to remove all instances of
'00:00:00.00' in my date column.

how would i write a python script to scan the entire page and delete all
instances of '00:00:00.00', would i use regular expressions?

umm. if you're using a database, why not filter out uninteresting dates either
in the SQL statement, or when you're building the page ?

</F>
 
L

Larry Bates

Kun said:
I have a python-cgi file that pulls data from an sql database, i am
wondering what is the easiest way to remove all instances of
'00:00:00.00' in my date column.

how would i write a python script to scan the entire page and delete all
instances of '00:00:00.00', would i use regular expressions?

You could use regular expressions or you might want to take a look at
Beautiful Soup http://www.crummy.com/software/BeautifulSoup
to parse the page and replace the offending text.

-Larry Bates
 
K

Kun

Fredrik said:
umm. if you're using a database, why not filter out uninteresting dates either
in the SQL statement, or when you're building the page ?

</F>
because in my sql database, the date is only 'date' (as in yyyy-mm-dd),
only when i extract it with my python-cgi does the date turn into
(yyyy-mm-dd 00:00:00.00), thus i figured the best way to fix this
problem is to parse it after the matter.

side note: the 'date' column is not formatted as datetime in the mysql
database.
 
F

Fredrik Lundh

Kun said:
because in my sql database, the date is only 'date' (as in yyyy-mm-dd),
only when i extract it with my python-cgi does the date turn into
(yyyy-mm-dd 00:00:00.00), thus i figured the best way to fix this
problem is to parse it after the matter.

you still make no sense. why not fix this in your python cgi script ?

</F>
 
K

Kun

Fredrik said:
you still make no sense. why not fix this in your python cgi script ?

</F>
i have the following python-cgi which extracts data from a mysql table,
how do i parse the date so that it doesn't display the time '00:00:00.00'?

print '<h1>Query Results</h1>'
try:
db = MySQLdb.connect(host="localhost", user="xxx", passwd="xxxx",
db="xxxx")
cursor = db.cursor()
sqlstring = (select + " FROM dir" + where + order_by + limit)
print sqlstring
cursor.execute(sqlstring)

numrows = cursor.rowcount
numcols = len(cursor.description)
#print sqlstring
#print "SQL statement used:<br>" + sqlstring

print """<table border="1" cellpadding="1" cellspacing="1">"""
print "<tr>"

for col in range(0, numcols):
print "<td><b>", cursor.description[col][0], "</b></td>"

print "</tr>"

for row in range(0,numrows):
record = cursor.fetchone()
print "<tr>"

for col in range(0, numcols):
print "<td>", record[col], "</td>"

print "</tr>"

except MySQLdb.OperationalError, message:
print "Error %d:<br>%s<br><br>" % (message[0], message[1])
print "SQL statement used:<br>" + sqlstring

print "</table>"
 
S

skip

Kun> i have the following python-cgi which extracts data from a mysql
Kun> table, how do i parse the date so that it doesn't display the time
Kun> '00:00:00.00'?

I have no idea which column in your table is a datetime object, but just
convert it to a date. For example:
2006-04-17

Skip
 
K

Kun

Kun> i have the following python-cgi which extracts data from a mysql
Kun> table, how do i parse the date so that it doesn't display the time
Kun> '00:00:00.00'?

I have no idea which column in your table is a datetime object, but just
convert it to a date. For example:

2006-04-17

Skip
assuming that my date column is 2, how would i parse out the date? the
example you gave is of you parsing out the current time, but how do you
parse out a pre-specified time that is extracted via sql? i don't think
something like dt.date() works because it doesn't work with a string?
correct me if i'm wrong.
 
T

Tim Chase

for col in range(0, numcols):
print "<td>", record[col], "</td>"

This is the point at which you want to intercept the column
data and make your change:

print "<td>", str(record[col]).replace("00:00:00.0", ""), "</td"

If it's possible/plausible that other fields might have such
a value reasonably, then you'd want to do a check, something
like


THEDATECOL = 42
for col in range(0, numcols):
foo = record[col]
if col == THEDATECOL:
foo = foo.replace("00:00:00.00", "")
print "<td>%s</td>" % foo

or alternatively

DATECOLUMNS = [3, 14]
for col in range(0, numcols):
foo = record[col]
if col in DATECOLUMNS:
foo = foo.replace("00:00:00.00", "")
print "<td>%s</td>" % foo

I don't know off the top of my head if your MySQL cursor
object supports metadata...something like the following
pseudocode:

for col in range(0, numcols):
foo = record[col]
if cursor.fieldtypes[col] == MYSQL_DATE:
foo = foo.replace("00:00:00.00", "")
print "<td>%s</td>" % foo

Adjust accordingly.

-tkc
 
K

Kun

Tim said:
for col in range(0, numcols):
print "<td>", record[col], "</td>"

This is the point at which you want to intercept the column data and
make your change:

print "<td>", str(record[col]).replace("00:00:00.0", ""), "</td"

If it's possible/plausible that other fields might have such a value
reasonably, then you'd want to do a check, something like


THEDATECOL = 42
for col in range(0, numcols):
foo = record[col]
if col == THEDATECOL:
foo = foo.replace("00:00:00.00", "")
print "<td>%s</td>" % foo

or alternatively

DATECOLUMNS = [3, 14]
for col in range(0, numcols):
foo = record[col]
if col in DATECOLUMNS:
foo = foo.replace("00:00:00.00", "")
print "<td>%s</td>" % foo

I don't know off the top of my head if your MySQL cursor object supports
metadata...something like the following pseudocode:

for col in range(0, numcols):
foo = record[col]
if cursor.fieldtypes[col] == MYSQL_DATE:
foo = foo.replace("00:00:00.00", "")
print "<td>%s</td>" % foo

Adjust accordingly.

-tkc
much thanks!
mu
 
S

skip

Kun> assuming that my date column is 2, how would i parse out the date?

No parsing required. Just get its date:

d = record[2].date()

The str() of a datetime.date object is a string in YYYY-MM-DD form.

Kun> the example you gave is of you parsing out the current time, but
Kun> how do you parse out a pre-specified time that is extracted via
Kun> sql? i don't think something like dt.date() works because it
Kun> doesn't work with a string? correct me if i'm wrong.

I think MySQLdb automatically returns datetime objects when the column data
type is date.

Skip
 
F

Fredrik Lundh

Kun> assuming that my date column is 2, how would i parse out the date?

No parsing required. Just get its date:

d = record[2].date()

The str() of a datetime.date object is a string in YYYY-MM-DD form.

or, in other words, change:

for col in range(0, numcols):
print "<td>", record[col], "</td>"

to

# convert to list, so we can replace columns, if needed
record = list(record)

# fix the date column
record[2] = record[2].date()
# add other fixups here

# print it out
for col in range(0, numcols):
print "<td>", record[col], "</td>"

the last two lines are of course better written as

for col in record:
print "<td>", col, "</td>"

</F>
 
K

Kent Johnson

Kun said:
because in my sql database, the date is only 'date' (as in yyyy-mm-dd),
only when i extract it with my python-cgi does the date turn into
(yyyy-mm-dd 00:00:00.00), thus i figured the best way to fix this
problem is to parse it after the matter.

It sounds like the problem is with the way you format the date for
output. Just omit the time.

Kent
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top