如何用Perl打开Unicode文件? [英] How can I open a Unicode file with Perl?

查看:91
本文介绍了如何用Perl打开Unicode文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用osql对数据库运行多个sql脚本,然后我需要查看结果文件来检查是否有任何错误发生。问题是Perl似乎并不喜欢结果文件是Unicode。

I'm using osql to run several sql scripts against a database and then I need to look at the results file to check if any errors occurred. The problem is that Perl doesn't seem to like the fact that the results files are Unicode.

我写了一个测试脚本来测试它,输出结果全部出来混乱:

I wrote a little test script to test it and the output comes out all warbled:

$file = shift;

open OUTPUT, $file or die "Can't open $file: $!\n";
while (<OUTPUT>) {
    print $_;
    if (/Invalid|invalid|Cannot|cannot/) {
        push(@invalids, $file);
        print "invalid file - $inputfile - schedule for retry\n";
        last;
    }            
}

任何想法?我尝试使用 decode_utf8 进行解码,但没有任何区别。我也试图在打开文件时设置编码。

Any ideas? I've tried decoding using decode_utf8 but it makes no difference. I've also tried to set the encoding when opening the file.

我认为问题可能是/ os>将结果文件放在UTF -16格式,但我不确定。当我在文本框中打开文件时,它只是告诉我'Unicode'。

I think the problem might be that osql puts the result file in UTF-16 format, but I'm not sure. When I open the file in textpad it just tells me 'Unicode'.

编辑:使用perl v5.8.8
编辑:十六进制转储:

Using perl v5.8.8 Hex dump:

file name: Admin_CI.User.sql.results
mime type: 

0000-0010:  ff fe 31 00-3e 00 20 00-32 00 3e 00-20 00 4d 00  ..1.>... 2.>...M.
0000-0020:  73 00 67 00-20 00 31 00-35 00 30 00-30 00 37 00  s.g...1. 5.0.0.7.
0000-0030:  2c 00 20 00-4c 00 65 00-76 00 65 00-6c 00 20 00  ,...L.e. v.e.l...
0000-0032:  31 00                                            1.


推荐答案

该文件大概在 UCS2-LE (或< a href =http://en.wikipedia.org/wiki/UTF-16/UCS-2 =noreferrer> UTF-16 格式)。

The file is presumably in UCS2-LE (or UTF-16 format).

C:\Temp> notepad test.txt

C:\Temp> xxd test.txt
0000000: fffe 5400 6800 6900 7300 2000 6900 7300  ..T.h.i.s. .i.s.
0000010: 2000 6100 2000 6600 6900 6c00 6500 2e00   .a. .f.i.l.e...

打开此类文件进行阅读时,需要指定编码:

When opening such file for reading, you need to specify the encoding:

#!/usr/bin/perl

use strict; use warnings;

my ($infile) = @ARGV;

open my $in, '<:encoding(UCS-2le)', $infile
    or die "Cannot open '$infile': $!";

请注意,开始时的 fffe BOM

这篇关于如何用Perl打开Unicode文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆