L
Leif B. Kristensen
Having recently started with Python, I've written this little function
to retrieve place parts from a database and concatenate them to a
string. While it certainly works, and is also considerably shorter than
the PHP code that I originally wrote, I'm pretty convinced that there
should be an even better way to do it. Can anybody show me how to write
the string concatenation part in a more Pythonesque syntax?
def get_place(x):
c=db.cursor()
c.execute("select place_lvl1, place_lvl2, \
place_lvl3, place_lvl4, place_lvl5 \
from place where place_id = %d" % (x))
res=c.fetchone()
place = ''
for i in range(5):
tmp = res
if tmp[:1] != '-' and len(tmp) != 0:
place = place + ', ' + (res)
return place[2:]
regards,
to retrieve place parts from a database and concatenate them to a
string. While it certainly works, and is also considerably shorter than
the PHP code that I originally wrote, I'm pretty convinced that there
should be an even better way to do it. Can anybody show me how to write
the string concatenation part in a more Pythonesque syntax?
def get_place(x):
c=db.cursor()
c.execute("select place_lvl1, place_lvl2, \
place_lvl3, place_lvl4, place_lvl5 \
from place where place_id = %d" % (x))
res=c.fetchone()
place = ''
for i in range(5):
tmp = res
if tmp[:1] != '-' and len(tmp) != 0:
place = place + ', ' + (res)
return place[2:]
regards,