2017-05-01 6 views
0

テキストファイルを1行ずつ編集するのは混乱します。列ツリーのキーワード条件(列ツリー:23,2,53、n ..)が増加するにつれて行を並べ替える方法キーワード条件を増やしてテキストファイルを編集する(coloumn tree)

set fp [open "data.txt" r] 
set filecontent [read $fp] 

Text file content: 
one, cat, 23,yes,check 
two, zebra, 2,yes,check 
tree, bird, 53,yes,check 
fourth, dog, 15,no,uncheck 
five, worm, 9,no,uncheck 
six, monkey, 41,yes,uncheck 
and so on.. 

Output text file content: 
two, zebra, 2,yes,check 
five, worm, 9,no,uncheck 
fourth, dog, 15,no,uncheck 
one, cat, 23,yes,check 
six, monkey, 41,yes,uncheck 
tree, bird, 53,yes,check 

ありがとうございます!

+0

本当に興味のあるデータは、CSV形式ですか?それとも、サンプルのようにカンマで区切られていますか? (実際のCSV形式には値にカンマを使用するための重要な引用規則がいくつかあります)。これは、実際のCSVファイルを解析するための標準パッケージがあるためです。 –

答えて

1

リストをすべて動作させる。 [lsort -integer -index 2 $lines_list]を使用してください。ここで、lines_listは行のリストであり、各行もリストです。 Tclはサブリストのリストを第3の整数サブリスト要素でソートします。

set fp [open "data.txt" r] 
set filecontent [read $fp] 
close $fp 

set text [split $filecontent \n] 
foreach line $text { 
    lappend lines_list [split $line ","] 
} 
set lines_list [lsort -integer -index 2 $lines_list] 
set text "" 
foreach line $lines_list { 
    append text [join $line ","] \n 
} 

set fout [open "out.txt" w] 
puts -nonewline $fout $text 
close $fout 
+0

ありがとうございました! –

関連する問題