我有 CSV 文件。我尝试阅读它并以逗号为基础将其拆分并将其放入HTML table
,但不起作用。
CSV 文件看起来像这样 -
姓名,号码,地址
abc,1,z
pqr,0,w
表格标题是名称,地址,数字 TD将是剩余的数据
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>"
答案 0 :(得分:0)
foreach (var item in duplicates)
{
if (item.sFolder == myObject.sFolder )
{
// Do stuff
break;
}
}
答案 1 :(得分: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
方法,我通常使用asXML
代替html文档。
文档: catch, close, csv (package), foreach, if, open, package, puts, return, set, struct::queue (package), tdom (package), while