当前位置:  开发笔记 > 编程语言 > 正文

如何使用Perl将CSV转换为HTML表格?

如何解决《如何使用Perl将CSV转换为HTML表格?》经验,为你挑选了4个好方法。

在我的代码中,我想以表格形式查看CSV中的所有数据,但它只显示最后一行.第1行和第2行怎么样?这是数据:

1,HF6,08-Oct-08,34:22:13,df,jhj,fh,fh,ffgh,gh,g,rt,ffgsaf,asdf,dd,yoawa,DWP,tester,Pattern
2,hf35,08-Oct-08,34:12:13,dg,jh,fh,fgh,fgh,gh,gfh,re,fsaf,asdf,dd,yokogawa,DWP,DWP,Pattern
3,hf35,08-Oct-08,31:22:03,dg,jh,fh,fgh,gh,gh,gh,rte,ffgsaf,asdf,dfffd,yokogawa,DWP,DWP,ghh

这是代码:

#! /usr/bin/perl
print "Content-type:text/html\r\n\r\n";
use CGI qw(:standard);
use strict;
use warnings;

my $line;
my $file;
my ($f1,$f2,$f3,$f4,$f5,$f6,$f7,$f8,$f9,$f10,$f11,$f12,$f13,$f14,$f15,$f16,$f17,$f18,$f19);

$file='MyFile.txt';
open(F,$file)||die("Could not open $file");
while ($line=)
{

($f1,$f2,$f3,$f4,$f5,$f6,$f7,$f8,$f9,$f10,$f11,$f12,$f13,$f14,$f15,$f16,$f17,$f18,$f19)= split ',',$line;

}

close(F);

print "";
print "";
print "";
print "FUSION SHIFT REPORT";
print "
"; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print "
RECORD No.TESTER No.DATETIMEDEVICE NAMETEST PROGRAMDEVICE FAMILYSMSLOTDIE LOTLOADBOARDTESTER SERIAL NUMBERTESTER CONFIGSMSLOTPACKAGESOCKETROOT CAUSE 1ROOT CAUSE 2ROOT CAUSE 3
$f1$f2$f3$f4$f5$f6$f7$f8$f9$f10$f11$f12$f13$f14$f15$f16$f17$f18$f19
"; print ""; print "";

Paul Dixon.. 14

您需要在里面输出表行在while循环,因为这是您读取行的位置.

所以改变代码吧

输出表头

逐行读取文件输出表行

输出表格页脚

如果稍微简化一下,这就是你的循环看起来如何......

while ($line=)
{  
    print "";
    my @cells= split ',',$line;
    foreach my $cell (@cells)
    {
       print "$cell";
    }
    print "";
}


小智.. 7

HTML :: Template会让您的生活更轻松.这是我的缩减模板.

#!/usr/local/bin/perl

use strict;
use warnings;

use HTML::Template;

my @table;
while (my $line = ){
    chomp $line;
    my @row = map{{cell => $_}} split(/,/, $line);
    push @table, {row => \@row};
}

my $tmpl = HTML::Template->new(scalarref => \get_tmpl());
$tmpl->param(table => \@table);
print $tmpl->output;

sub get_tmpl{
    return <






TMPL
}

__DATA__
1,HF6,08-Oct-08,34:22:13,df,jhj,fh,fh,ffgh,gh,g,rt,ffgsaf,asdf,dd,yoawa,DWP,tester,Pattern
2,hf35,08-Oct-08,34:12:13,dg,jh,fh,fgh,fgh,gh,gfh,re,fsaf,asdf,dd,yokogawa,DWP,DWP,Pattern
3,hf35,08-Oct-08,31:22:03,dg,jh,fh,fgh,gh,gh,gh,rte,ffgsaf,asdf,dfffd,yokogawa,DWP,DWP,ghh


AmbroseChape.. 6

$f1,$f2,$f3,$f4

任何时候你都会看到类似闹钟的代码应该消失.使用数组.



1> Paul Dixon..:

您需要在里面输出表行在while循环,因为这是您读取行的位置.

所以改变代码吧

输出表头

逐行读取文件输出表行

输出表格页脚

如果稍微简化一下,这就是你的循环看起来如何......

while ($line=)
{  
    print "";
    my @cells= split ',',$line;
    foreach my $cell (@cells)
    {
       print "$cell";
    }
    print "";
}



2> 小智..:

HTML :: Template会让您的生活更轻松.这是我的缩减模板.

#!/usr/local/bin/perl

use strict;
use warnings;

use HTML::Template;

my @table;
while (my $line = ){
    chomp $line;
    my @row = map{{cell => $_}} split(/,/, $line);
    push @table, {row => \@row};
}

my $tmpl = HTML::Template->new(scalarref => \get_tmpl());
$tmpl->param(table => \@table);
print $tmpl->output;

sub get_tmpl{
    return <






TMPL
}

__DATA__
1,HF6,08-Oct-08,34:22:13,df,jhj,fh,fh,ffgh,gh,g,rt,ffgsaf,asdf,dd,yoawa,DWP,tester,Pattern
2,hf35,08-Oct-08,34:12:13,dg,jh,fh,fgh,fgh,gh,gfh,re,fsaf,asdf,dd,yokogawa,DWP,DWP,Pattern
3,hf35,08-Oct-08,31:22:03,dg,jh,fh,fgh,gh,gh,gh,rte,ffgsaf,asdf,dfffd,yokogawa,DWP,DWP,ghh



3> AmbroseChape..:
$f1,$f2,$f3,$f4

任何时候你都会看到类似闹钟的代码应该消失.使用数组.


这应该是一个评论.它并不试图回答这个问题.

4> Greg Hewgill..:

请允许我用一个较小的例子来演示.

my $f;
while ($line = ) {
    $f = $line;
}
print $f;

上面的代码将读取文件F的每一行,将每一行分配给变量$f.每次分配新行时,前一行都会被覆盖.当它到达文件末尾时,它会打印出$f一次的值.

my $f;
while ($line = ) {
    $f = $line;
    print $f;
}

上面的代码print在循环内部,因此print将为输入的每一行运行.您将需要对代码进行类似的修改以获得您期望的输出.

推荐阅读
保佑欣疼你的芯疼
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有