2013-09-16 26 views
1

我解析HTML文件,並通過使用代碼寫作CSV在Perl

open(MYFILE ,">>Test.csv"); print MYFILE qq|"$name","$table","$picture"\n|; close MYFILE;

其中變量$表中包含如下::

內容在單獨的細胞

編寫源代碼

<table cellspacing=0" cellpadding="0" style="text-align: center;"> 
    <tbody> 
    <tr> 
    <td valign="middle" class="td1"> 
    <p class="p1"><span class="s1"><b><i><u><font size="5">Brand New in Package 64 GB Black/Silver USB 2.0 Flash Drive</font></u></i></b></span></p> 
    <ul class="ul1"> 
    <li class="li2">Do not be fooled by the low price! The flash drives are EXCELLENT quality and I can assure you that you will be more than pleased</li><li class="li2">True Capacity</li> 
    <li class="li2">&nbsp;I am the fastest seller you will find and having your item shipped to you as fast as possible is my first priority</li> 
    <li class="li2">Most purchases will be shipped within 24 hours if ordered Monday - Friday</li> 
    <li class="li2">If you have any questions please feel free to ask!</li></ul></td></tr></tbody></table><center><br></center><center><br></center><center><font size="7" color="#00429a">Need more space? Check out my 128 GB Listings for as low as </font><font size="7" color="#ad001f"><b><u>$33.99</u></b></font><font size="7" color="#00429a">!!</font></center><p></p>" 

其使得CSV佔據和重疊的下一個細胞

我怎樣才能讓他們只打印一個單元格。 需要一些指導.Thanks

更新

@TLP謝謝,但如果我使用此代碼

my $csv = Text::CSV->new ({ binary => 1 }) # should set binary attribute. 
or die "Cannot use CSV: ".Text::CSV->error_diag(); 
open my $fh, ">:encoding(utf8)", "Test.csv" or die "Test.csv: $!"; 
$csv->print ($name,$table); 
close $fh; 

其仍顯示錯誤爲「預期的字段是一個數組引用」

請給我一些建議

更新:

感謝SZG,我還有一個疑問,我怎麼能換行添加到它,我想按行打印行,如果我使用

$csv->print($fh,["\n"]); 

預期其仍然沒有工作。我認爲我在某些地方是錯的

+5

use ['Text :: CSV'](http://search.cpan.org/perldoc?Text%3A%3ACSV)? – TLP

+1

對於Text :: CSV :: print,您有錯誤的參數。試試'$ csv-> print($ fh,[$ name,$ table])' – AKHolland

+0

謝謝,非常感謝。 :) – Balakumar

回答

3

有人懷疑你從未使用過打開的$ fh文件句柄。是的,csv_print需要一個文件句柄和一個數組引用。

在您的原始代碼中,您似乎想要附加到現有的CSV文件open(MYFILE ,">>Test.csv")。所以我也改變了新的代碼。

$csv = Text::CSV->new ({ binary => 1 }) # should set binary attribute. 
or die "Cannot use CSV: ".Text::CSV->error_diag();      
open $fh, ">>:encoding(utf8)", "Test.csv" or die "Test.csv: $!";   
$csv->print($fh, [$name, $table]);          
close $fh;                
+0

謝謝SzG,我還有一個疑問,我該如何添加一個換行符,我想逐行打印, 如果我使用 '$ csv-> print($ fh,[「\ n」]);' 它仍然沒有按預期工作。 我想我在某些地方是錯的,。 – Balakumar