Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django unicode to string

Tags:

python

django

I stored ip in sqlite database as normal string, but when i try to get that ip from database

>>> p = Info.objects.get(id = 1)
>>> p.ip
>>> u"[u'10.10.10.1']"`

How I can get normal string "10.10.10.1"

like image 930
big Avatar asked Feb 20 '23 03:02

big


1 Answers

What you have there is a list with just one element.

To get the element from the list you could use p.ip[0].

That would give you a unicode object, then if you want to convert it to a string (just some bytes) you should use encode, i.e. p.ip[0].encode('utf8')

like image 169
Facundo Casco Avatar answered Feb 25 '23 16:02

Facundo Casco