我正在處理腳本以基於csv文件批量重命名和複製圖像。 csv由第1列:舊名稱和第2列:新名稱組成。我想將csv文件用作perl腳本的輸入,以便它檢查舊名稱並使用新名稱將副本複製到新文件夾中。 (我認爲)我遇到的問題與圖像有關。它們包含UTF8字符,如SS等。當我運行該腳本,它打印出這一點:Barfu├ƒg├ñsschen它應該是Barfußgässchen和以下錯誤:使用csv文件複製/重命名utf8名稱的圖像
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";
它打印出這一點:艾爾特÷小號
這是我的整個腳本,可有人向我解釋我要去哪裏錯誤? (它不是最乾淨的代碼,因爲我正在測試的東西)。
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;
關於你的第一個片段:.pl文件本身是用utf8編碼的嗎? 'use utf8' pragma告訴Perl你的源代碼是用utf8編寫的。它不涉及數據。 – simbabque
你打印輸出到哪裏?一個Linux shell?另外,你是如何創建文件? –
我可以確認你的第一個代碼片段在LANG設置爲'en_GB.UTF-8'並且Putty設置爲UTF-8的情況下可以在我的Linux shell上正常工作。我在同一個shell中使用VIM創建了該文件。 –