Perl   发布时间:2022-04-07  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了perl – 使用csv文件使用utf8名称复制/重命名图像大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在编写一个脚本来批量重命名和复制基于csv文件的图像. csv由第1列组成:旧名称和第2列:新名称.我想使用csv文件作为perl脚本的输入,以便它检查旧名称并使用新名称将副本复制到新文件夹中. (我认为)我与图像有关的问题.它们包含像ß等utf8字符.当我运行脚本时,它打印出来:Barfu├ƒg├ñsschen它应该是Barfußgässchen并且出现以下错误:
Unsuccessful stat on filename containing newline at C:/Perl64/lib/File/Copy.pm line 148,<$INFILE> line 1.
Copy failed: No such file or directory at X:\Script directory\correction.pl line 26,<$INFILE> line 1.

我知道它与Binmode utf8有关,但即使我尝试一个简单的脚本(在这里看到它:How can I output UTF-8 from Perl?):

use Strict;
use utf8;
my $str = 'Çirçös';
binmode(STDOUT,":utf8");
print "$str\n";

它打印出来:Ãirþ÷s

这是我的整个剧本,有人可以向我解释我哪里出错了吗? (它不是最干净的代码,因为我正在测试的东西).

use Strict;
use warnings;
use File::Copy;
use utf8;

my $inputfile  = shift || die "give input!\n";
#my $outputfile = shift || die "Give output!\n";

open my $INFILE,'<',$inputfile   or die "In use / not found :$!\n";
#open my $OUTFILE,'>',$outputfile  or die "In use / not found :$!\n";

binmode($INFILE,":encoding(utf8)");

#binmode($OUTFILE,":encoding(utf8)");

while (<$INFILE>) {
s/"//g;
my @elements = split /;/,$_;

my $old = $elements[1];
my $new = "new/$elements[3]";
binmode STDOUT,':utf8';
print "$old | $new\n";

copy("$old","$new") or die "Copy failed: $!";
#copy("Copy.pm",\*STDOUT);

#   my $output_line = join(";",@elements);
#    print $OUTFILE $output_line;
#print "\n"
}

close $INFILE;
#close $OUTFILE;

exit 0;

解决方法

您需要确保该过程的每个步骤都使用UTF-8.

创建输入CSV时,需要确保将其保存为UTF-8,最好不使用BOm. Windows记事本将添加BOM,因此请尝试使用记事本,这样可以更好地控制编码.

您还遇到默认情况下Windows控制台不符合UTF-8的问题.请参见Unicode characters in Windows command line – how?.使用chcp 65001设置代码页或不更改STDOUT编码.

就您的代码而言,关于新行的第一个错误可能是由于CSV中的尾随新行.在while(< $INFILE>)之后添加chomp(){

更新:

要“解决”您需要使用正确的语言环境对文件名进行编码的文件 – 请参阅How do you create unicode file names in Windows using PerlWhat is the universal way to use file I/O API with unicode filenames?.假设您使用的是Western 1252 / LaTin,这意味着您的复制命令将如下所示:

copy(encode("cp1252",$old),encode("cp1252",$new))

此外,您的打开还应编码文件名:

open my $INFILE,$inputfilE)

更新2:

当你在DOS窗口中运行时,删除binmode(STDOUT,“:utf8”);并保留默认代码页.

大佬总结

以上是大佬教程为你收集整理的perl – 使用csv文件使用utf8名称复制/重命名图像全部内容,希望文章能够帮你解决perl – 使用csv文件使用utf8名称复制/重命名图像所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。