469,951 Members | 2,485 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,951 developers. It's quick & easy.

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

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?
Apr 17 '06 #1
13 1282
> 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

Apr 17 '06 #2
Kun wrote:
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
Apr 17 '06 #3
"Kun" wrote:
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>

Apr 17 '06 #4
Kun
Fredrik Lundh wrote:
"Kun" wrote:
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>

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.
Apr 17 '06 #5
"Kun" wrote:
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>

Apr 17 '06 #6
Kun
Fredrik Lundh wrote:
"Kun" wrote:
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>

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>"
Apr 17 '06 #7

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:
import datetime
dt = datetime.datetime.now()
print dt 2006-04-17 18:19:38.698925 print dt.date()

2006-04-17

Skip
Apr 17 '06 #8
Kun
sk**@pobox.com wrote:
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:
>>> import datetime
>>> dt = datetime.datetime.now()
>>> print dt 2006-04-17 18:19:38.698925 >>> print dt.date()

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.
Apr 17 '06 #9
> 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

Apr 17 '06 #10
Kun
Tim Chase wrote:
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
Apr 18 '06 #11

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
Apr 18 '06 #12
sk**@pobox.com wrote:

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>

Apr 18 '06 #13
Kun wrote:
Fredrik Lundh wrote:
"Kun" wrote:
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 ?

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
Apr 18 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Randall Skelton | last post: by
reply views Thread by clintonG | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.