From: Larry W. V. <lv...@gm...> - 2012-05-02 14:24:00
|
So, I have a file that generates text of the form: itemname;relationship name;date;user;"item 2" itemname2;relationship name;date;user;"item 3 item 4 item 5" and so forth. I am wanting to write some code that reads these csv records in, one at a time, and then loop through the list in the last column creating a series of lines of the format itemname --> list item 0; itemname --> list item 1; : itemname -> list item n; I tried to extract just column 1 and 4 from this file using the example program csvcut - however, it did not handle the multiple line properly. I read over the docs, and didn't see an input function - just the iscomplete function. so i wrote this: package require Tcl package require csv proc readcsvrecord { fd } { gets $fd buffer # puts stderr "rcr 1: $buffer" while { [csv::iscomplete $buffer ] == 0} { gets $fd buff2 set buffer ${buffer}${buff2} # puts stderr "rcr 2: $buffer" } return $buffer } puts "graph ci {" set next [readcsvrecord stdin] # puts stderr "eof returns [eof stdin]" while { ! [eof stdin] } { # puts stderr "Record is $next" set l [csv::split $next "\;"] # puts stderr "list is $l" # error here foreach item [lindex $l 4] { puts "[lindex $l 0]->$item;" } set next [readcsvrecord stdin] } puts "}" ~ This sort of works. However, when generating my output (see "error here") I have a problem when the item has white space in it - the white space is turning one item into multiples. Two questions: 1. Am I missing anything in the handling of the actual csv file? 2. Am I forgetting something basic to Tcl in handling the csv column containing white space? I presume this is nothing csv specific, but instead something basic that I have forgotten since I don't write a lot of code in Tcl any more... sigh. I really want to make certain my handling of the CSV records, and columns, is correct. I was concerned about whether the backslash was needed before the separator character; I didnt want Tcl to choke when it sees it. |