[root@dr-mysql01 ~]# cat x5.pl
use Encode;
use JSON;
use URI::Escape;
use LWP::Simple;
my $host = "http://ip.taobao.com/service/getIpInfo.php?ip=202.101.172.35";
my $content = get($host);
print "$content is $content
";
$content =~ s/\u([0-9a-fA-F]{4})/pack("U",,hex($1))/eg;
print "$content is $content
";
use Data::Dumper;
my $json_out= Dumper($content);
print "$json_out is $json_out
"
You have mail in /var/spool/mail/root
[root@dr-mysql01 ~]# perl x5.pl
$content is {"code":0,"data":{"country":"u4e2du56fd","country_id":"CN","area":"u534eu4e1c","area_id":"300000","region":"u6d59u6c5fu7701","region_id":"330000","city":"u676du5ddeu5e02","city_id":"330100","county":"","county_id":"-1","isp":"u7535u4fe1","isp_id":"100017","ip":"202.101.172.35"}}
Wide character in print at x5.pl line 12.
$content is {"code":0,"data":{"country":"中国","country_id":"CN","area":"华东","area_id":"300000","region":"浙江省","region_id":"330000","city":"杭州市","city_id":"330100","county":"","county_id":"-1","isp":"电信","isp_id":"100017","ip":"202.101.172.35"}}
$json_out is $VAR1 = "{"code":0,"data":{"country":"x{4e2d}x{56fd}","country_id":"CN","area":"x{534e}x{4e1c}","area_id":"300000","region":"x{6d59}x{6c5f}x{7701}","region_id":"330000","city":"x{676d}x{5dde}x{5e02}","city_id":"330100","county":"","county_id":"-1","isp":"x{7535}x{4fe1}",
"isp_id":"100017","ip":"202.101.172.35"}}";
如何用python将utf8编码的16进制字符串转换成对应的文字?
>>> '中国'.decode('utf8')
u'u4e2du56fd'
>>> u'u4e2du56fd'.encode('utf8')
'xe4xb8xadxe5x9bxbd'
>>>