免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 7099 | 回复: 8
打印 上一主题 下一主题

再对perl中文编码提出问题! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-03-15 17:27 |只看该作者 |倒序浏览
我现在遇到这个问题 比较棘手
请看 (test.txt是一个包含中文的文本)
test1.pl
#!/usr/bin/perl
open (FILET,"/home/test.txt");
@lines=<FILET>;
close(FILET);
for ($i=0; $i < @lines ;$i++) {
        print $lines[$i];
        print "\n";
}

$str = "你好";
print $str;
直接运行perl test1.pl > test1res.txt
然后打开 http://test1res.txt
则  test.txt中的中文显示正常 但是"你好"处显示乱码


test2.pl
#!/usr/bin/perl
use encoding 'gb2312' , STDIN => 'gb2312', STDOUT => 'gb2312';
open (FILET,"/home/test.txt");
@lines=<FILET>;
close(FILET);
for ($i=0; $i < @lines ;$i++) {
        print $lines[$i];
        print "\n";
}

$str = "你好";
print $str;
直接运行perl test2.pl > test2res.txt
然后打开 http://test2res.txt
则  test.txt中的中文显示成为类似"x\{2e45} x\{3e0651f}" 的样子 但是"你好"处显示正常

test3.pl
#!/usr/bin/perl
open (FILET,"/home/test.txt");
@lines=<FILET>;
close(FILET);
for ($i=0; $i < @lines ;$i++) {
        print $lines[$i];
        print "\n";
}

$str = "你好";
$str = decode('gb2312',$str);
print $str;
直接运行perl test3.pl > test3res.txt
然后打开 http://test3res.txt
则  换成网页uft8格式时 "你好"处显示正常 test.txt中文还是不正常

我不知是系统问题 还是perl的问题 总之很奇怪

论坛徽章:
0
2 [报告]
发表于 2006-03-15 18:04 |只看该作者
原帖由 hawkyw 于 2006-3-15 17:27 发表
我现在遇到这个问题 比较棘手
......
我不知是系统问题 还是perl的问题 总之很奇怪


Both!
System's encoding => GB2312, that's why you can see 中文 from file in the STDOUT
Perl self (perl self code) => support UTF-8, that's why Encode should be used
Depending on what OUTPUT devices you want to display.

Pls ref. to http://bbs.chinaunix.net/viewthread.php?tid=698736&highlight=%D6%D0%CE%C4

Best, ulmer

论坛徽章:
0
3 [报告]
发表于 2006-03-15 20:57 |只看该作者
请先运行 locale 命令查看你的系统环境是 utf-8 还是gb2312(或者gbk)?
test.txt 的编码是 utf8 还是 gb2312(或者 gbk)?

论坛徽章:
0
4 [报告]
发表于 2006-03-16 10:14 |只看该作者

运行结果是

#locale
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

test.txt应该是gb2312 的 因为我在浏览器中用非gb2312的编码打开是乱码
____________________________________________________
#!/usr/bin/perl
use encoding 'gb2312' , STDIN => 'gb2312', STDOUT => 'utf8';

use Encode;
print "Content-type: text/html\n\n";
#use encoding 'latin1';
$str = encode("gb2312","你好");

print $str;
print <<EOM;
<html>
<head>
<title>"$str"</title>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312" >
</head>
EOM
open (FILET,"/home/mmk/test.txt");
@lines=<FILET>;
close(FILET);
for ($i=0; $i < @lines ;$i++) {
        print $lines[$i]);
        print "\n";
}
这个代码输出后 "你好"处要让网页变成utf-8才能正确显示 其他部分显示正常
要怎么样才能都显示正常呢?

论坛徽章:
0
5 [报告]
发表于 2006-03-16 18:49 |只看该作者
试试:
#!/usr/bin/perl

open (FILET,"test.txt");
binmode FILET,":encoding(gb2312)";
@lines=<FILET>;
close(FILET);
for ($i=0; $i < @lines ;$i++) {
        print $lines[$i];
        print "\n";
}

$str = "你好";
print $str;

论坛徽章:
0
6 [报告]
发表于 2006-03-16 20:03 |只看该作者
原帖由 hawkyw 于 2006-3-16 10:14 发表
test.txt应该是gb2312 的 因为我在浏览器中用非gb2312的编码打开是乱码


Just type the following cmd and will see what encoding of your test.txt
file test.txt
file -i test.txt

then you can concentrate the suitable encode/decode.

Best, ulmer

论坛徽章:
0
7 [报告]
发表于 2006-03-23 15:20 |只看该作者

结果

# file test.txt
test.txt: data

#file -i test.txt
test.txt: application/octet-stream

论坛徽章:
0
8 [报告]
发表于 2006-03-23 16:16 |只看该作者

xiexie

谢谢问题解决 不过还是 不很明白
我是这样解决的
#!/usr/bin/perl
use encoding "gb2312";
open (FILET,"test.txt");
binmode FILET,":encoding(gb2312)";
@lines=<FILET>;
close(FILET);
for ($i=0; $i < @lines ;$i++) {
        print $lines[$i];
        print "\n";
}

$str = "你好";
print $str;

这样中文都能正确显示了 至于为什么 我还是 不是很了解

论坛徽章:
0
9 [报告]
发表于 2006-03-23 16:52 |只看该作者
强制为gb2312了吧?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP