XML 파일을 편집하고 일반 텍스트 파일로 변환하는 디렉토리가 있습니다. - 숫자가있는 행이 둘 이상인 경우 새 파일에 저장해야합니다. 별도의 새 줄에. XML 파일은 다음과 같습니다 경우펄의 파일에 새 줄 쓰기
예를 들어, :
이Bounding box for object 1 "PAScar" (Xmin, Ymin) - (Xmax, Ymax) : (335, 248) - (450, 305)
Bounding box for object 2 "PAScar" (Xmin, Ymin) - (Xmax, Ymax) : (32, 251) - (188, 319)
Bounding box for object 3 "PAScar" (Xmin, Ymin) - (Xmax, Ymax) : (472, 245) - (574, 290)
이 내 코드의 관련 부분입니다 :
다음<![CDATA[335 248 450 305
32 251 188 319
472 245 574 290
]]>
나는 변환 된 파일은 다음과 같이 할
if($file =~ /\.xml$/i)
{ $i += 1;
open (MYFILE, $file);
$newlines = "";
my $objnum;
$objnum = 0;
while (my $row = <MYFILE>)
{
my $line;
$line = "";
if($row =~ m/\d+(?:\s+\d+){3}$/)
{
$objnum=$objnum+1;
if($row =~ /CDATA/)
{
my($prefix, $suff, $nums) = split(/\[/, $row);
$line = $nums
}
else
{
$line = $row;
}
my ($x1, $y1, $x2, $y2) = split(" ",$line);
$newlines = $newlines.'\n'.'Bounding box for object '.$objnum.' "PAScar" (Xmin, Ymin) - (Xmax, Ymax) : ('.$x1.', '.$y1.') - ('.$x2.', '.$y2.')';
}
}
close MYFILE;
my $tempfile;
my $newfile;
$tempfile = "D:/PATH/temp.txt";
open (my $tmp, '>>:crlf', $tempfile) or die "** can't open temp file:(**";
print $tmp $newlines;
close $tmp;
copy $tempfile, $file;
unlink $tempfile;
문제는 변환 된 파일이 다음과 같이 표시됩니다.
\nBounding box for object 1 "PAScar" (Xmin, Ymin) - (Xmax, Ymax) : (335, 248) - (450, 305)\nBounding box for object 2 "PAScar" (Xmin, Ymin) - (Xmax, Ymax) : (32, 251) - (188, 319)\nBounding box for object 3 "PAScar" (Xmin, Ymin) - (Xmax, Ymax) : (472, 245) - (574, 290)
왜 개행 문자가 없습니까? Windows에서 실행 중입니다. 나는 이것이 Notepad ++ 문제가 아니라는 것을 안다. 그것은 메모장과 워드 패드에서도 이와 같이 나타납니다. 나는 \r\n
을 사용했지만 새로운 라인을 만들지 못했습니다. @lines = split("\n", $newlines);
다음 나는 각 반복에서 새롭게 파일을 연 foreach $line (@lines)
루프 실행 :
그런 다음, 단순히 새로운 파일로 $newlines
을 인쇄하는 것보다, 내가이 일을 시도
foreach $line (@lines)
{ open ($tmp, '>>', $tempfile) or die "** can't open temp file:(**";
print $tmp $line;
}
을하지만 같은 얻을 결과는 \n
이 없지만이 줄을 제외하고는 모두 같은 줄에 있습니다.
어떻게해야합니까?
$ newlines = $ newlines. \ n ".Bounding ...과 같이 ** DOUBLE ** 따옴표에 \ n 묶는 것은 어떨까요? –