[Tutor] How can I see properly my korean.

Young-gyu Park jackup at gmail.com
Thu May 31 13:44:32 CEST 2007


>         user = User.objects.get(userID__exact=user_id)
>         user.blog_set.all()
>         blogData = user.blog_set.get(id__exact=user.id)
>         section_list = blogData.section_set.all()
>         latest_content_list = blogData.content_set.all
> ().order_by('-pub_date')[:5]
>
>         blog = dict()
>         blog['currentPost'] = {"dateIndex":0, "postIndex":0}
>
>         blog['title'] = blogData.name.encode('utf-8')
>         blog['description'] = blogData.description.encode('utf-8')
>
>         sections = list()
>         for section in section_list:
>                 sections.append({"title":section.name.strip(), "link":"
> www.hideout.com.br"})
>         blog['sections'] = sections
>
>         links = list()
>         link = dict()
>         links.append({"title":user_id, "link":"www.hideout.com.br"})
>         links.append({"title":"hideout", "link":"www.hideout.com.br"})
>         links.append({"title":"hideout", "link":"www.hideout.com.br"})
>         links.append({"title":"hideout", "link":"www.hideout.com.br"})
>
>         blog['links'] = links
>
>         comments = list()
>         comment = dict()
>         comment['dateTime'] = "10:43 7/20/2004"
>         comment['author'] = "ygp"
>         comment['comment'] = "blah"
>         comments.append(comment)
>
>         items = list()
>         for content in latest_content_list:
>                 item = dict()
>                 item['title'] = content.subject
>                 item['body'] = content.content
>                 item['author'] = user.name
>                 item['permalink'] = "perma link"
>                 item['time'] = "13234 23423423"
>                 item['comments'] = comments
>                 items.append(item)
>
>
>         blog['items'] = items
>
>         fileHandle = open ( '/var/chroot/www/htdocs/django/js/model.js',
> 'w' )
>         fileHandle.write( codecs.BOM_UTF8 )
>         print >> fileHandle, 'var blog = '
>         print >> fileHandle, blog
>         fileHandle.close()
>
This is the part of my whole source code.

I try to convert the python dict into javascript array.

Because they have same syntax.

So when I print out the the dict, this is valid javascript array.

And then I parsed the javascript array to display html page. But when I
print out the python dict.

the __str__ function don't have any encode routine. so I guess this problem
is caused.

The below is my wonder.

Do I need to make my own fuction which to convert the dict value to utf-8?

Or Is there any other way to display dict's korean letter properly?



On 5/31/07, Kent Johnson <kent37 at tds.net> wrote:
>
> Young-gyu Park wrote:
>
> >             fileHandle = open (
> >     '/var/chroot/www/htdocs/django/js/model.js', 'w' )
> >             fileHandle.write( codecs.BOM_UTF8 )
> >             print >> fileHandle, 'var blog = '
> >             print >> fileHandle, blog
> >             fileHandle.close()
> >
> >
> > this is the file model.js
> >
> >
> >     var blog =
> >     {'description': '\xec\xb9\xb4\xed\x86\xa8\xeb\xa6\xad
> ....
> >     <http://www.hideout.com.br>', 'title': '\xed\x9b\x84\xec\x9b\x90'}]}
> >
> >
> > What I want to do is to see properly the letter not this letter
> '\xec\x9d'
> >
> > Can anyone who know solution let me know how to do kindly?
>
> You haven't shown us enough code. Where does the variable blog come from?
>
> This is a hard question to answer because there are so many ways to get
> confused. How did you display the file? It is possible that it contains
> the correct characters but the method you are using to display them
> shows them as \x escapes. For example the Python interpreter will do this.
>
> It looks like you are using a JSON encoder to create the data. Which
> one? Here is an example using the version of SimpleJSON that is bundled
> with Django. It does what you want but it's a little tricky to be sure:
>
> In [3]: from django.utils.simplejson import dumps
>
> This is Python so I can use \x escapes to define the string; the actual
> string is UTF-8:
>
> In [4]: data = {'description': '\xec\xb9\xb4\xed\x86\xa8\xeb\xa6\xad
> \xed\x91\xb8\xeb\xa6\x84\xed\x84\xb0'}
>
> If I ask the interpreter for the value directly, it shows it with
> escapes. (Technically, the interpreter prints repr(value) for any value
> it is asked to display; for strings, repr() inserts \x escapes so the
> result is printable ASCII text.)
>
> In [7]: data['description']
> Out[7]: '\xec\xb9\xb4\xed\x86\xa8\xeb\xa6\xad
> \xed\x91\xb8\xeb\xa6\x84\xed\x84\xb0'
>
> On the other hand, if I ask the interpreter explicitly to print the
> value, the \x escapes are not inserted and the correct characters are
> shown:
>
> In [8]: print data['description']
> 카톨릭 푸름터
>
> The parameter ensure_ascii=False prevents the JSON serializer from
> converting the individual bytes of UTF-8 to \u escapes.
>
> Here again, showing the converted data directly uses repr() and shows \x
> escapes:
>
> In [6]: dumps(data, ensure_ascii=False)
> Out[6]: '{"description": "\xec\xb9\xb4\xed\x86\xa8\xeb\xa6\xad
>
> If I print the result, I can see that it contains the correct characters:
>
> In [17]: print dumps(data, ensure_ascii=False)
> {"description": "카톨릭 푸름터"}
>
> Kent
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070531/9a7797a9/attachment-0001.html 


More information about the Tutor mailing list