2016-12-23 7 views
1

私はCSVファイルを持っています。私はそれを読んで、コンマベースでそれを分割し、それをHTML tableに入れてみましたが、うまくいきませんでした。 CSVファイルcsvファイルから読み込んだ後、htmlにデータを出力するには?

テーブルヘッダは、名前、住所なりwは、数 TDが残りのデータ

なり、this- 名前、番号、アドレス ABC、1、Z PQR、0のように見えます
set FileName "file.csv" 
catch {set fptr [open $FileName r]} 
set contents [read -nonewline $fptr] 
set splitCont [split $contents ","] 

foreach line $splitCont { 
    if { [regexp -nocase {^\s*Name} $line val] } { 
    puts $filep "<TR><TH valign=top>$val</TH>\n" 
    } 
puts $filep "</TR>" 
} 
close $fptr 
puts $filep "</TABLE>" 
+2

何が問題なのですか?どのようなアウトプットを得ていますか、何を得たいですか?今のところ、少なくとも適切な 'td' /' th'タグがありません。加えて、テーブルには 'h2'が含まれてはいけません(これらの行を回ります)。 – Seth

答えて

0
set htmlFile "aborted_jobs.html" 
set filep [open $htmlFile w] 

puts $filep "<HTML><HEAD><TITLE>Aborted Jobs Summary</TITLE></HEAD><BODY>" 
puts $filep "<TABLE border=1 cellpadding=4 align=center>\n" 
puts $filep "<H2 align=center>Aborted Jobs Summary</H2>" 

set FileName "aborted_jobs.csv" 
catch {set fptr [open $FileName r]} 
set contents [read -nonewline $fptr] 
set splitCont [split $contents "\n"] 

foreach line $splitCont { 
    set cellList [split $line ","] 
    set tableHeader 0 
    if { [regexp -nocase {InstanceName} $cellList] } { 
     set tableHeader 1 
    } 
    puts $filep "<TR>" 
    foreach data $cellList { 
      if {$tableHeader} { 
       puts $filep "<TH valign=top>$data</TH>" 
      } else { 
       puts $filep "<TD>$data</TD>" 
      } 
    } 
    puts $filep "</TR>" 
} 
close $fptr 
puts $filep "</TABLE>" 
0

このコードは少し長いですが、それが拡張するとあなたはcsvファイルを読んだり、手でHTMLを書く場合よりもエラーを回避するためにはるかに簡単です。

package require tdom 
package require csv 
package require struct::queue 

set doc [dom createDocument html] 
set root [$doc documentElement] 
$root appendXML "<HEAD><TITLE>Aborted Jobs Summary</TITLE></HEAD>" 
set body [$doc createElement body] 
$root appendChild $body 
set h2 [$doc createElement h2] 
$h2 appendChild [$doc createTextNode "Aborted Jobs Summary"] 
$h2 setAttribute align center 
$body appendChild $h2 
set table [$doc createElement table] 
$table setAttribute border 1 cellpadding 4 align center 
$body appendChild $table 

set FileName "aborted_jobs.csv" 
if {[catch {open $FileName} f]} { 
    # just rethrow unless you want to handle it 
    return -code error $f 
} 

::struct::queue q 
::csv::read2queue $f q 
close $f 

set tr [$doc createElement tr] 
$table appendChild $tr 

foreach header [q get] { 
    set th [$doc createElement th] 
    $th appendChild [$doc createTextNode $header] 
    $th setAttribute valign top 
    $tr appendChild $th 
} 

while {[q size] > 0} { 
    set tr [$doc createElement tr] 
    $table appendChild $tr 

    foreach data [q get] { 
     set td [$doc createElement td] 
     $td appendChild [$doc createTextNode $data] 
     $tr appendChild $td 
    } 
} 

set htmlFile "aborted_jobs.html" 
set filep [open $htmlFile w] 
$doc asXML -channel $filep 
close $filep 

tdomドキュメントオブジェクトも同様asHTML方法を持って、私は通常でも、HTMLドキュメントの代わりにasXMLを使用しています。

ドキュメント: catchclosecsv (package)foreachifopenpackageputsreturnsetstruct::queue (package)tdom (package)while

関連する問題