python - formatting the output for an SQL query -
i have following script use sql query required information,it works fine information not in expected format,please see below expected output,it should space seperated list no new characters ("\n") @ end,how output in required format?
db = mysqldb.connect(host="dbhost",user="username",port=3339,passwd="passwordname",db="dbname") # prepare cursor object using cursor() method cursor = db.cursor() gerritlist = [] # prepare sql query gerrits database. sql_get = """select gerrit_id dbname.gerrit_submit_table (si='%s' , component='%s')"""%(si,component) #print sql_get rows = cursor.execute(sql_get) gerrits = cursor.fetchall() print "gerrits db" print gerrits
output:-
gerrits db (('1258565',), ('1279604',))
expectedoutput:-(space seperated list ,no new character(\n) @ end)
1258565 1279604
at end, this:
print ' '.join(item[0] item in gerrits),
or this:
print ' '.join(item[0] item in gerrits).rstrip('\r\n')
or if want store values in list (eg. gerritlist
variable have):
gerritlist = [item[0] item in gerrits]
Comments
Post a Comment