开发者

how to print a dict which has japanese word using python

开发者 https://www.devze.com 2023-02-28 01:28 出处:网络
this is my code : import xlrd,sys data = xlrd.open_workbook(\'a.xls\') boss = data.sheets()[3] print boss.row_values(3)[1]

this is my code :

import xlrd,sys
data = xlrd.open_workbook('a.xls')
boss = data.sheets()[3]
print boss.row_values(3)[1]

it can show the right word :

キャプションサンプル。てすてす。改行もしたいよ

but because the a.xls has many data , so i append this data to a dict in 开发者_JAVA百科python ,

like this :

stage_dict[stage_number]['boss'][boss_number]['name'] =  boss_row_list[4]

and show it :

print stage_dict

but it show this :

'\uff77\uff6c\uff8c\uff9f\uff7c\uff6e\uff9d\uff7b\uff9d\uff8c\uff9f\uff99\u3002\u3066\u3059\u3066\u3059\u3002\u6539\u884c\u3082\u3057\u305f\u3044\u3088\u3002'

so what can i do ,

how to print a dict using the right Coding,

thanks

updated:

this is a simple demo :

#coding:utf-8

a={0:"キャプションサンプル。てすてす。改行もしたいよ"}
b="キャプションサンプル。てすてす。改行もしたいよ"
print a,b


You cannot. print(somedict) will always print the repr() of the dict which is valid python code using escape sequences.

However, since printing a dict should be a debug-only thing anyway this is not really a problem. Displaying a value from the dict should work.


You can derive your own class from dict and override __str__ method:

# -*- coding: utf-8 -*-

class MyDict(dict):
    def __str__(self):
        return "{"+", ".join(["%s: %s" % (key, self[key]) for key in self])+"}" 

a = {0:"Velmi žluťoučký kůň"}
b = MyDict({0:"Velmi žluťoučký kůň"})
c = "Velmi žluťoučký kůň"
print(a)
print(b)
print(c)

Prints:

{0: 'Velmi \xc5\xbelu\xc5\xa5ou\xc4\x8dk\xc3\xbd k\xc5\xaf\xc5\x88'}
{0: Velmi žluťoučký kůň}
Velmi žluťoučký kůň

The derived class will behave exactly the same as dict, but it will print using the method you specify.


Printing an object in Python will print how the object is represented in English text. Hence the result you are experiencing.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号