Django queryset csv encode

130 views
Skip to first unread message

hito koto

unread,
May 25, 2014, 10:55:51 PM5/25/14
to django...@googlegroups.com
Hi,
I want to encode in utf8 but don't know how can i to ?

i have the errors is here:

AttributeError: 'tuple' object has no attribute 'encode'

My code is here :
 name = [name.encode("utf8") for name in Staff.objects.filter(id = 3).values_list('user_name')]


Kelvin Wong

unread,
May 26, 2014, 12:15:41 AM5/26/14
to django...@googlegroups.com
Try

name = [name.encode("utf8") for name in Staff.objects.filter(id = 3).values_list('user_name', flat=True)]

The other returns one-tuples in a list


K

hito koto

unread,
May 26, 2014, 12:23:47 AM5/26/14
to django...@googlegroups.com
Hi,

I7m try that's this time not the error but output the  ['\xe5\x93\x88\xe6\x96\xaf\xe6\x9c\x9d\xe9\xad\xaf']






2014年5月26日月曜日 13時15分41秒 UTC+9 WongoBongo:

Kelvin Wong

unread,
May 26, 2014, 1:10:44 AM5/26/14
to django...@googlegroups.com
Python 3.4.0 (default, Mar 20 2014, 12:50:31) 
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> b'\xe5\x93\x88\xe6\x96\xaf\xe6\x9c\x9d\xe9\xad\xaf'.decode('utf8')
'哈斯朝魯'

K

hito koto

unread,
May 26, 2014, 1:24:36 AM5/26/14
to django...@googlegroups.com
Hi,

I'm try tha's but results:

>>> b'\xe5\x93\x88\xe6\x96\xaf\xe6\x9c\x9d\xe9\xad\xaf'.decode('utf8')
u'\u54c8\u65af\u671d\u9b6f'




2014年5月26日月曜日 14時10分44秒 UTC+9 WongoBongo:

WongoBongo

unread,
May 26, 2014, 2:26:03 AM5/26/14
to django...@googlegroups.com
Python 2.7.6 (default, Jan 13 2014, 04:26:18) 
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x = u'\u54c8\u65af\u671d\u9b6f'
>>> print(x)
哈斯朝魯
>>> import locale
>>> locale.getdefaultlocale()[1]
'UTF-8'
>>>

K

hito koto

unread,
May 26, 2014, 2:34:12 AM5/26/14
to django...@googlegroups.com
Hi,
I have this:
>>> x = name
>>> import locale
>>> locale.getdefaultlocale()[1]
'UTF8'
>>> print x
['\xe5\x93\x88\xe6\x96\xaf\xe6\x9c\x9d\xe9\xad\xaf']

after i try this :

>>> name
['\xe5\x93\x88\xe6\x96\xaf\xe6\x9c\x9d\xe9\xad\xaf']
>>> type(name)
<type 'list'>

Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'list' object has no attribute 'decode'







2014年5月26日月曜日 15時26分03秒 UTC+9 WongoBongo:

Kelvin Wong

unread,
May 26, 2014, 4:46:13 AM5/26/14
to django...@googlegroups.com
Okay, I re-read your first post. You said that you wanted to get a UTF-8 string.

name = [name.encode("utf8") for name in Staff.objects.filter(id = 3).values_list('user_name', flat=True)]

This variable 'name' now contains a UTF-8 encoded string in a list-like object.

Python 2.7.6 (default, Jan 13 2014, 04:26:18) 
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> name = ['\xe5\x93\x88\xe6\x96\xaf\xe6\x9c\x9d\xe9\xad\xaf']
>>> print name[0]
哈斯朝魯
>>>

Copy and paste the code. Don't retype it.

K

hito koto

unread,
May 26, 2014, 5:12:14 AM5/26/14
to django...@googlegroups.com
Hi,

this is output the one name, but i want to output all name, so i can't logic 
>>> name[0].encode("Shift_JIS")







2014年5月26日月曜日 17時46分13秒 UTC+9 WongoBongo:

Kelvin Wong

unread,
May 26, 2014, 5:39:20 AM5/26/14
to django...@googlegroups.com
If you want all the user_name's in Staff as UTF-8:

staff = Staff.objects.all()
staff_list = [s.user_name.encode('utf-8') for s in staff]

I'm not familiar with the encoding you mentioned in your last message. Try it.

K


On Monday, May 26, 2014 2:12:14 AM UTC-7, hito koto wrote:
Hi,

hito koto

unread,
May 26, 2014, 8:27:48 AM5/26/14
to django...@googlegroups.com
Hi, WongoBongo,

Thanks



2014年5月26日月曜日 18時39分20秒 UTC+9 WongoBongo:
Reply all
Reply to author
Forward
0 new messages