MySQLdb Help

2013-09-17 Thread Venkat Addala
Hi all,


  I'm new to python, i am connecting mysql database with python. now i want
to do sanitation on the database, like to remove "\n", extra spaces blah
blah.. and update it back to mysql database. i was trying somthing, here is
my code, can you please provide me solution for this..

#!/usr/bin/python
import MySQLdb as mdb

con = mdb.connect('localhost', 'root', 'pass@123', 'workbench');

with con:
  cur = con.cursor()
  cur.execute("SELECT descrip FROM table LIMIT 1")
  rows = cur.fetchall()
  for row in rows:
row_new = row[0].split("\n", " ")
row = "".join(row_new)
  print row
exit(0)
--

Regards
Boffin
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: MySQLdb Help

2013-09-17 Thread Venkat Addala
hey Ben,

Thanks for your solution, i am done with this before, now i'm having
problem after deleting \n and extra spaces, i want to update it back it
again to mysql db.

For this i tried using regular expression  check this code;

# -*- coding: utf-8 -*-
import re
import MySQLdb

pattern = re.compile('@(.*)@.*$')

conn = MySQLdb.connect(
  host='localhost', user='root',
  passwd='pass123', db='workbench', charset='utf8')

cursor = conn.cursor()

cursor.execute("""
SELECT * FROM gene where gene_id=13 AND `descrip` regexp "^@.*@.*$"
LIMIT 1""")
rows = cursor.fetchall()
for row in rows:
  rows_new = pattern.match(row[1])
if rows_new.group(1) is not None:
  cursor.execute("""
  update gene set descrip = %s where descrip = %s""",
(rows_new.group(1), row[0]))
  #cursor.execute("""
  #update gene set descrip = %s where descrip = %s""",
(rows_new.group(1), row[0]))

  conn.close()


--
Regards
Boffin





On Tue, Sep 17, 2013 at 1:05 PM, Ben Finney wrote:

> Venkat Addala  writes:
>
> >   I'm new to python
>
> Welcome! Congratulations on choosing Python for programming.
>
> > i am connecting mysql database with python. now i want to do
> > sanitation on the database, like to remove "\n", extra spaces blah
> > blah.. and update it back to mysql database.
>
>
> > i was trying somthing, here is my code, can you please provide me
> > solution for this..
>
> Thank you for providing a small, complete example.
>
> You should also describe what behaviour you expect, and what behaviour
> you're getting instead. What happens, and how are you expecting it to be
> different?
>
> > #!/usr/bin/python
> > import MySQLdb as mdb
> >
> > con = mdb.connect('localhost', 'root', 'pass@123', 'workbench');
>
> There's no need to end Python statements with a semicolon; it's only
> confusing to do it.
>
> >   rows = cur.fetchall()
> >   for row in rows:
>
> You never use ‘rows’ for anything else, so you may as well forget it and
> just iterate directly over the return value::
>
> for row in cur.fetchall():
>
> > row_new = row[0].split("\n", " ")
>
> Read the documentation for ‘str.split’ to see what is wrong with the
> above call.
>
>
> You might also be interested in the ‘python-tutor’ forum, specially
> designed for beginners with basic questions about Python
> http://mail.python.org/mailman/listinfo/tutor>
> http://dir.gmane.org/gmane.comp.python.tutor>.
>
> Good hunting to you!
>
> --
>  \ “Sunday: A day given over by Americans to wishing that they |
>   `\  themselves were dead and in Heaven, and that their neighbors |
> _o__)were dead and in Hell.” —Henry L. Mencken |
> Ben Finney
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Remove \n, " " from tuple.

2013-09-18 Thread Venkat Addala
Hi all,

I have a tuple in this format, which is retrived using MySQLdb, now i want
to remove \n and extra spaces in this.

13L, 'ssDsC', 'CEs5s1, DsC', 'srylscetsmight\nghtscetylsse', '3', '3q25.1',
151531861L, 151546276L, '+', '1 kjafhkfhlad\fdfdsdf ghtscetylsse \ncontends
the sctivity of dsfdk srylsmine N-scetyltrsnsfersse thst scts ss s cstslyst
inbiotrsnsformstion fghjfg for srylsmine snd bvhghbh smine csrcinogens.
Bgskjdg in ssDsC dfkdkdf in bresst snd prostrste csncer.', '', 'Msdhurs
\nsgdfgvslingegowds', dstetime.dste(2013, 6, 14), None



--
Regards
Venkat Addala  <http://in.linkedin.com/in/venkataddala/>
Computational Biologist
  O__  
 _/ /`_ ---
(*) \(*) --
-- 
https://mail.python.org/mailman/listinfo/python-list