【问题标题】:writing new lines in a file in perl在 perl 中的文件中写入新行
【发布时间】:2015-01-02 05:14:02
【问题描述】:

我有一个目录,我在其中编辑 XML 文件并将它们转换为常规文本文件,其中 - 如果多行带有数字,则必须将它们保存在新文件中单独的新行中。

例如,如果 XML 文件如下所示:

<![CDATA[335 248 450 305
32 251 188 319
472 245 574 290
]]>  

然后我希望转换后的文件看起来像这样:

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)

这是我的代码的相关部分:

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,但也没有换行。

然后,我不是简单地将$newlines 打印到新文件中,而是尝试这样做:@lines = split("\n", $newlines);,然后运行foreach $line (@lines) 循环,在每次迭代中重新打开文件:

foreach $line (@lines)
        {   open ($tmp, '>>', $tempfile) or die "** can't  open temp file:( **";
            print $tmp $line;
        }

但我得到了相同的结果,除了这次没有\ns,但所有内容都在同一行。

怎么办?

【问题讨论】:

  • 如何将 \n 括在 DOUBLE 引号中,例如:$newlines = $newlines."\n".'Bounding ...

标签: regex perl file-io newline


【解决方案1】:

换行:

$newlines = $newlines.'\n'.'Bounding box for object '.$objnum.' "PAScar" (Xmin, Ymin) - (Xmax, Ymax) : ('.$x1.', '.$y1.') - ('.$x2.', '.$y2.')';

到:

$newlines = $newlines."\n".'Bounding box for object '.$objnum.' "PAScar" (Xmin, Ymin) - (Xmax, Ymax) : ('.$x1.', '.$y1.') - ('.$x2.', '.$y2.')';

即您需要将 \n 放在双引号中,“\n”,而不是单引号,'\n',以使其成为换行符。

【讨论】:

    猜你喜欢
    • 2012-09-26
    • 1970-01-01
    • 2012-06-15
    • 1970-01-01
    • 2011-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-22
    相关资源
    最近更新 更多