1

I'm querying a database and get this text:

"('username', '192.168.1.1', datetime.datetime(2013, 1, 3, 7, 18))"

I want to split into list like this:

['username', '192.168.1.1', datatime.datetime(2013, 1, 3, 7, 18)]

So, which way is the best way? I'm using Python 2.4.

Alex Riley
  • 169,130
  • 45
  • 262
  • 238
  • 1
    do you want datetime.datetime(..) to become a datetime object, or should it stay a string? – wastl Nov 10 '14 at 16:15
  • datetime is between quotes? Because this way a simple `split` operation won't work. – Alberto Coletta Nov 10 '14 at 16:17
  • If it weren't for the `datetime` bit, I'd suggest `thetext[1:-1].split(",")` for this. Perhaps you could use a [regex](https://docs.python.org/2/library/re.html) to replace the commas inside the brackets with something else (such as `;`) first? – GoBusto Nov 10 '14 at 16:19
  • 1
    What database are you using, and why do you get a string back? – jonrsharpe Nov 10 '14 at 16:20
  • possible duplicate of [Python - lexical analysis and tokenization](http://stackoverflow.com/questions/2358890/python-lexical-analysis-and-tokenization) – ivan_pozdeev Nov 10 '14 at 16:22
  • @SoundOfSilent What format is the data officially is? And why is it in such a format? The best solution is to get the data that's already split into its meaningful parts in the first place. – ivan_pozdeev Nov 10 '14 at 16:27

3 Answers3

7

One way would be to use eval and list:

>>> import datetime
>>> string = "('username', '192.168.1.1', datetime.datetime(2013, 1, 3, 7, 18))"
>>> list(eval(string))
['username', '192.168.1.1', datetime.datetime(2013, 1, 3, 7, 18)]

eval evaluates the string as a Python expression, here producing a tuple containing the two strings and a datetime instance. list converts this tuple to a list.


Caution: eval will potentially execute any valid Python code, even malicious code. There are safer alternatives such as ast.literal_eval which could be used if you're not sure whether you trust what's in your database.

Alex Riley
  • 169,130
  • 45
  • 262
  • 238
0

You can use maxsplit parameter of the split function:

your_string = "('username', '192.168.1.1', datetime.datetime(2013, 1, 3, 7, 18))"
your_string[1:-1].split(",", 2)

# Returns:
# ["'username'", " '192.168.1.1'", ' datetime.datetime(2013, 1, 3, 7, 18)']
Alberto Coletta
  • 1,563
  • 2
  • 15
  • 24
0

As noted in jonrsharpe comment, it looks like you are falling in a trap you have just digged yourself.

Any correct database can give you the individual columns, and if you correctly do the query, you get them in proper format.

Assuming you can use a DB-API 2.0 described in PEP 249, and assuming conn is an open connection to your database, you should do something like :

curs = conn.cursor()
curs.execute('SELECT name, ip, dat FROM tab')
t = curs.fetchone()

and t should directly be the tuple ('username', '192.168.1.1', datatime.datetime(2013, 1, 3, 7, 18)) (use list(t) if you need a list ...)

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252