- 論壇徽章:
- 0
|
本帖最后由 百分百好牛 于 2011-05-10 17:14 編輯
我有個(gè)很古老的程序(沒(méi)有source code了),會(huì)生成一個(gè)log文件,設(shè)這個(gè)文件名叫l(wèi)og.txt,這個(gè)文件encoding很奇怪,用ultraedit32 打開(kāi) 如下:- 0000000: 5400 4400 4400 3e00 2000 4c00 6f00 6100 T.D.D.>. .L.o.a.
復(fù)制代碼 如果我用notepad打開(kāi),再save as呢,就成了下面這個(gè),其實(shí)是 utf-16的- 0000000: fffe 5400 4400 4400 3e00 2000 4c00 6f00 ..T.D.D.>. .L.o.
復(fù)制代碼 但是,這個(gè)log.txt不能直接處理,比如說(shuō)按我下面的這個(gè)方法去查找,會(huì)失敗,因?yàn)閷?shí)際上,每次讀出來(lái)的行是 T.D.D.不是我想要的 TDD。
- open STDIN, "< log.txt";
- while(<>)
- {
- if (/TDD/)
- {
- # Add my logic.
- }
- }
復(fù)制代碼 現(xiàn)在我想把他轉(zhuǎn)化成一個(gè)標(biāo)準(zhǔn)的utf16或者utf8,卻總是提示失敗,如下:
perl.exe open.pl utf-16le utf8 log.txt
- use strict;
- use warnings;
- use Encode;
- # read arguments
- my $enc_in = shift || die 'pass file encoding as first parameter';
- my $enc_out = shift || die 'pass STDOUT encoding as second parameter';
- print STDERR "going to read files as encoded in: $enc_in\n";
- print STDERR "going to write to standard output in: $enc_out\n";
- die "no files :-(\n" unless @ARGV;
- binmode STDOUT, ":encoding($enc_out)"; # latin1, cp1252, utf8, UTF-8
- print STDERR map "* $_\n", Encode->encodings; # list loaded encodings
- for ( @ARGV ) { # process files
- open my $fh, "<:encoding($enc_in)", $_ or die "open $_: $!";
- print while <$fh>;
- close $fh;
- }
- print STDERR map "* $_\n", Encode->encodings; # more encodings now
復(fù)制代碼 錯(cuò)誤的提示總類似于
UTF-16LE : Partial character at open.pl line 18, <$fh> line 2011.
希望有經(jīng)驗(yàn)的兄弟過(guò)來(lái)幫忙看看。
perl 5.1
windows 2008 |
|