在perl中写CSV [英] Writing CSV in perl
问题描述
我使用代码
解析html文件并在单独的单元格中编写源代码 open(MYFILE,>>> Test.csv);
print MYFILE qq |$ name,$ table,$ picture\\\
|;
close MYFILE;
其中变量$ table包含以下内容:
< table cellspacing = 0cellpadding =0style =text-align:center;>
< tbody>
< tr>
< td valign =middleclass =td1>
< p class =p1>< span class =s1>< b& ;< / u>< / i>< / b>< / p> ;< / span>< / p>
< ul class =ul1>
< li class =li2>不要被低价骗!驱动器是非常好的质量,我可以向您保证,您将非常高兴< / li>< li class =li2>真实容量< / li>
< li class =li2> < / li>
< li class =li2>我们是您发现最快的卖家,并且尽可能快地将您的商品发送给您。如果在周一至周五订购,则在24小时内
< li class =li2>如果您有任何问题,请随时提出问题!< / li>< / ul>< / td>< / tr>< / tbody> < / table>< center>< br>< / center>< center>< br>< / center>< center>< font size =7color =#00429a ;需要更多的空间?查看我的128 GB列表,最低为< / font>< font size =7color =#ad001f>< b>< u> $ 33.99< / u>< / b& < / font>< font size =7color =#00429a> !!< / font>< / center>< p>< / p>
这将使CSV占据并重叠下一个单元格
我可以让他们只打印一个单元格。
需要一些指导.Thanks
更新
@TLP谢谢,但如果我使用这个代码
my $ csv = Text :: CSV-> new({binary = > 1})#应该设置二进制属性
或者不能使用CSV:.Text :: CSV-> error_diag();
打开我的$ fh,>:encoding utf8),Test.csv或dietest.csv:$!;
$ csv-> print($ name,$ table);
close $ fh;
其仍显示为期望字段为数组引用的错误
请给我一些建议
更新:
感谢SzG,可以添加一个换行符,如果我使用
$ csv-> print($ fh,[\\\
]);
它仍然无法正常工作。我想我在一些地方错了
这是可疑的,你从来没有使用打开的$ fh文件句柄。是的,csv_print需要一个文件句柄和一个数组引用。
在你的原始代码中,你似乎想将附加到现有的CSV文件 open(MYFILE,>>> Test.csv)
。
$ csv = Text :: CSV-> new({binary = > 1})#应设置二进制属性。
或die无法使用CSV:.Text :: CSV-> error_diag();
open $ fh,>>:encoding(utf8),Test.csv或dieTest.csv:$!
$ csv-> print($ fh,[$ name,$ table]);
close $ fh;
I am parsing a html file and writing the source codes in seperate cells
by using the code
open(MYFILE ,">>Test.csv");
print MYFILE qq|"$name","$table","$picture"\n|;
close MYFILE;
where the variable $table contains the content below::
<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"> 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>"
which is making the CSV to occupy and overlapping the next cells
how can i make them to print in only one cell. Need some guidance .Thanks
update
@TLP Thanks but if i use this code
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;
its still showing an error as "Expected fields to be an array ref"
please give me some suggestion
update :
Thanks SzG, I have another doubt how can i add a newline to it, i want to print it in row by row, if i use
$csv->print($fh,["\n"]);
its still not working as expected. i think i am wrong in some where
It was suspicious that you never used the opened $fh filehandle. And yes, csv_print needs a filehandle and an array-ref.
In your original code it seemed you wanted to append to an existing CSV file open(MYFILE ,">>Test.csv")
. So I changed the new code that way as well.
$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;
这篇关于在perl中写CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!