使用PERL将BOM插入CSV文件 [英] Insert BOM to a CSV File using PERL

查看:231
本文介绍了使用PERL将BOM插入CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个问题,使我的CSV文件可读。
我目前正在尝试使用PERL。这是我的代码:

Hi I am having a problem with making my CSV file readable. I am currently trying to do it using PERL. Here's my line of code:

#!/usr/bin/perl

$infile = @ARGV[0];
$outfile = @ARGV[1];

open(INFILE,"$infile") || die "cannot open input file : $infile : ";

open(OUTFILE,">$outfile") || die "cannot open output file";

$/="undef";

while(<INFILE>)

{

  $temp=$_;

}

close(INFILE);

  print OUTFILE "\x{feff}".$temp;

close(OUTFILE);

但是,CSV文件仍然无法读取。
我可以做什么来插入BOM吗?
谢谢!

However, the CSV file is still unreadable. Is there anything that I can do to insert BOM? Thanks!

推荐答案

在我们这样做之前,让我告诉你,BOMs在大多数情况下是一个令人难以置信的痛苦,并应尽可能避免。它们在技术上只需要UTF-16编码。 BOM是Unicode字符U + FEFF。它在UTF-8中编码为 EF BB BF ,在UTF-16LE中为 FF FE ,UTF-作为 FE FF 。你似乎假设你的输入是UTF-16BE,在这种情况下你可以直接写入字节:

Before we do this, let me tell you that BOMs are an incredible pain in most cases, and should be avoided wherever possible. They are only technically necessary with UTF-16 encodings. The BOM is the Unicode character U+FEFF. It is encoded in UTF-8 as EF BB BF, in UTF-16LE as FF FE, and UTF-16BE as FE FF. It seems you are assuming that your input is UTF-16BE, in that case you could write the bytes directly:

open my $in,  "<:raw", $ARGV[0] or die "Can't open $ARGV[0]: $!";
open my $out, ">:raw", $ARGV[1] or die "Can't open $ARGV[1]: $!";

print $out "\xFE\xFF";
while (<$in>) {
    print $out $_;
}

但是它可能会更好地解码和再次编码输出,显式指定BOM为字符:

But it would probably be better to decode and the encode the output again, and explicitly specify the BOM as a character:

open my $in,  "<:encoding(UTF-16BE)", $ARGV[0] or die "Can't open $ARGV[0]: $!";
open my $out, ">:encoding(UTF-16BE)", $ARGV[1] or die "Can't open $ARGV[1]: $!";

print $out "\N{U+FEFF}";
while (<$in>) {
    print $out $_;
}

这篇关于使用PERL将BOM插入CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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