AppleScript名:CSVのParse 5(ASOC) |
–Created By Shane Stanley 2015/03/12 –Commented & Arranged By Takaaki Naganoya 2015/03/12 use scripting additions use framework "Foundation" set theString to "cust1,\"prod,1\",season 1, cust1,prod1,season2, cust2,prod1,event1,season1 cust2,prod3,event1,season 1" its makeListsFromCSV:theString commaIs:"," –> {{"cust1", "prod,1", "season 1"}, {"cust1", "prod1", "season2"}, {"cust2", "prod1", "event1", "season1"}, {"cust2", "prod3", "event1", "season 1"}} –CSV Parser ASOC ver (Translated from "ASObjCExtras.framework" Objective-C version) on makeListsFromCSV:theString commaIs:theComma set theRows to {} –最終的に出力するデータ(2D Listになる) set newLineCharSet to current application’s NSCharacterSet’s newlineCharacterSet() –改行キャラクタ set importantCharSet to current application’s NSMutableCharacterSet’s characterSetWithCharactersInString:("\"" & theComma) –カンマ importantCharSet’s formUnionWithCharacterSet:newLineCharSet set theNSScanner to current application’s NSScanner’s scannerWithString:theString theNSScanner’s setCharactersToBeSkipped:(missing value) –データ末尾を検出するまでループ repeat while (theNSScanner’s isAtEnd() as integer = 0) set insideQuotes to false set finishedRow to false set theColumns to {} set currentColumn to "" –すべての行を処理終了するまでループ(行内部の処理) repeat while (not finishedRow) set {theResult, tempString} to theNSScanner’s scanUpToCharactersFromSet:importantCharSet intoString:(reference) –log {"theResult", theResult, "tempString", tempString} if theResult as integer = 1 then set currentColumn to currentColumn & (tempString as text) –log {"currentColumn", currentColumn} –データ末尾検出 if theNSScanner’s isAtEnd() as integer = 1 then if currentColumn is not "" then set end of theColumns to currentColumn set finishedRow to true else –データ末尾ではない場合 set {theResult, tempString} to theNSScanner’s scanCharactersFromSet:newLineCharSet intoString:(reference) if theResult as integer = 1 then if insideQuotes then –ダブルクォート文字内の場合 set currentColumn to currentColumn & (tempString as text) else –ダブルクォート内ではない場合 if currentColumn is not "" then set end of theColumns to currentColumn set finishedRow to true end if else –行末文字が見つからない場合 set theResult to theNSScanner’s scanString:"\"" intoString:(missing value) if theResult as integer = 1 then –ダブルクォート文字が見つかった場合 if insideQuotes then –ダブルクォート文字内の場合 set theResult to theNSScanner’s scanString:"\"" intoString:(missing value) if theResult as integer = 1 then set currentColumn to currentColumn & "\"" else set insideQuotes to (not insideQuotes) end if else –ダブルクォート文字内ではない場合 set insideQuotes to (not insideQuotes) end if else –ダブルクォート文字が見つからなかった場合 set theResult to theNSScanner’s scanString:theComma intoString:(missing value) –カンマの検索 if theResult as integer = 1 then if insideQuotes then set currentColumn to currentColumn & theComma else set end of theColumns to currentColumn set currentColumn to "" theNSScanner’s scanCharactersFromSet:(current application’s NSCharacterSet’s whitespaceCharacterSet()) intoString:(missing value) end if end if end if end if end if end repeat if (count of theColumns) > 0 then set end of theRows to theColumns –行データ(1D List)をtheRowsに追加(2D List) end repeat return theRows end makeListsFromCSV:commaIs: |
More from my site
(Visited 176 times, 1 visits today)
iTunesからMusic.appへの移行時に失われたトラック情報を取り戻す方法 – 桃源老師のつぶやき says:
[…] CSVのParse 5(ASOC)にあるAppleScriptを保存する。 筆者の場合は、csvParse5という名前を使った。 […]