0

この質問は、私の前の質問に続くものです。CSVをいくつかのリスト(applescript)に変換する方法は?

約4000行のcsvファイルがあります。それをインポートして各行をリストのアイテムに作成し、次に各行を独自のアイテムのリストにします。

例:(私は数字を使用していますが、私が持っているデータにはテキストも含まれており、ほとんどのエントリは1文字を超えており、一部のエントリも空白です)

1,2,3,4,5,6
6,5,4,3,2,1
7,8,9,0,7,6
3,4,4,5,3,1

になります

{"1,2,3,4,5,6","6,5,4,3,2,1","7,8,9,0,7,6","3,4,4,5,3,1"}

になります

{{"1","2","3","4","5","6"}{"6","5","4","3","2","1"}{"7","8","9","0","7","6"}{"3","4","4","5","3","1"}}

今私がしたいのは次のとおりです。

各リストから特定のアイテムを削除します。たとえば、各リストから2番目のアイテム、つまり「2」、「5」、「8」、「4」を削除します。

特定のアイテムで計算を実行します。たとえば、アイテム5に2を掛けます。

また、私のデータのいくつかの数字の最後に+11または+ 17が付いています。これを一致するゼロの量に置き換える方法を知りたいので、たとえば5002+6の場合は5002000000にします

現在のコードは次のとおりです。

-- Choosing your file
set csvDevices to "testfile.csv"

-- Reading file to memory
set csvDevices to read csvDevices

-- Creating Records (Single Lines)
set csvDevicesRecords to paragraphs of csvDevices

-- Remove Title Line
set csvDevicesRecords to rest of csvDevicesRecords

-- Make each line into a list
set csvDevicesValues to {}
set AppleScript's text item delimiters to ","
repeat with i from 1 to length of csvDevicesRecords
    set end of csvDevicesValues to text items of (item i of csvDevicesRecords)
end repeat
set AppleScript's text item delimiters to ""

上記のすべてが理にかなっていることを願っています。

4

3 に答える 3

1

サブルーチンを自分で書いて自分に代わって実行するのであれば、それほど難しいことではありません。リクエストしたのはこの3つです。他の数学演算 (加算、減算、および除算) を実行できるように、他のサブルーチンを追加する必要があります。MultiplyItemNumberByValue() サブルーチンを複製し、演算子を適切なものに変更するだけで、それ以上変更しなくても機能するはずです。

幸運を。

set listOfLists to {{"1", "2+2", "3", "4", "5", "6"}, {"6", "5", "4", "3", "2", "1"}, {"7", "8", "9", "0", "7", "6"}, {"3", "4", "4", "5+3", "3", "1"}}

-- before performing any other operation, expand all zeros
set listOfLists to expandZeros(listOfLists)

-- remove item 2 from every sublist
set listOfLists to removeItemNumber(listOfLists, 2)

-- multiply item 1 of every sublist by 5
set listOfLists to multiplyItemNumberByValue(listOfLists, 1, 5)




(****************** SUBROUTINES ******************)
on multiplyItemNumberByValue(listOfLists, itemNumber, theValue)
    set newList to {}
    repeat with i from 1 to count of listOfLists
        set thisList to item i of listOfLists
        set thisItem to item itemNumber of thisList
        set newValue to (thisItem as number) * theValue
        set item itemNumber of thisList to (newValue as text)
        set end of newList to thisList
    end repeat
    return newList
end multiplyItemNumberByValue

on removeItemNumber(listOfLists, itemNumber)
    set newList to {}
    repeat with i from 1 to count of listOfLists
        set thisList to item i of listOfLists
        if itemNumber is equal to 1 then
            set newSublist to items 2 thru end of thisList
        else if itemNumber is equal to (count of thisList) then
            set newSublist to items 1 thru (itemNumber - 1) of thisList
        else
            set newSublist to items 1 thru (itemNumber - 1) of thisList & items (itemNumber + 1) thru end of thisList
        end if
        set end of newList to newSublist
    end repeat
    return newList
end removeItemNumber

on expandZeros(listOfLists)
    set newList to {}
    repeat with i from 1 to count of listOfLists
        set thisList to item i of listOfLists
        set newSublist to {}
        repeat with j from 1 to count of thisList
            set subItem to item j of thisList
            if subItem contains "+" then
                set x to offset of "+" in subItem
                if x is equal to 0 or x is equal to 1 or x is equal to (count of subItem) then
                    set end of newSublist to subItem -- do nothing
                else
                    set a to text 1 thru (x - 1) of subItem
                    set b to text (x + 1) thru end of subItem
                    repeat (b as number) times
                        set a to a & "0"
                    end repeat
                    set end of newSublist to a
                end if
            else
                set end of newSublist to subItem -- do nothing
            end if
        end repeat
        set end of newList to newSublist
    end repeat
    return newList
end expandZeros

これが他の数学サブルーチンです...

on divideItemNumberByValue(listOfLists, itemNumber, theValue)
    set newList to {}
    repeat with i from 1 to count of listOfLists
        set thisList to item i of listOfLists
        set thisItem to item itemNumber of thisList
        set newValue to (thisItem as number) / theValue
        set item itemNumber of thisList to (newValue as text)
        set end of newList to thisList
    end repeat
    return newList
end divideItemNumberByValue

on addValueToItemNumber(listOfLists, itemNumber, theValue)
    set newList to {}
    repeat with i from 1 to count of listOfLists
        set thisList to item i of listOfLists
        set thisItem to item itemNumber of thisList
        set newValue to (thisItem as number) + theValue
        set item itemNumber of thisList to (newValue as text)
        set end of newList to thisList
    end repeat
    return newList
end addValueToItemNumber

on subtractValueFromItemNumber(listOfLists, itemNumber, theValue)
    set newList to {}
    repeat with i from 1 to count of listOfLists
        set thisList to item i of listOfLists
        set thisItem to item itemNumber of thisList
        set newValue to (thisItem as number) - theValue
        set item itemNumber of thisList to (newValue as text)
        set end of newList to thisList
    end repeat
    return newList
end subtractValueFromItemNumber
于 2013-02-19T18:22:24.667 に答える
0

Nigel Garvey の CSV からリストへのコンバーターは、私が見た中で最高のものです。

次のように呼び出すことができます。

    set filePath to "/Users/You/Desktop/myCsv.csv"
    set csvData to do shell script "cat " & quoted form of filePath
    set csvDevicesLists to csvToList(csvData, {trimming:true})

--> {{"1", "2", "3", "4", "5", "6"}, {"6", "5", "4", "3", "2", "1"}, {"7", "8", "9", "0", "7", "6"}, {"3", "4", "4", "5", "3", "1"}}

次のようにゼロをパディングできます。

set xxx to {{"1", "2", "3+2", "4", "52+3", "6"}, {"6", "5", "4+5", "3", "2", "1"}}
repeat with i from 1 to count of xxx
    if (item i of xxx as text) contains "+" then
        set temp to {}
        repeat with anItem in item i of xxx
            set end of temp to (do shell script "echo " & quoted form of anItem & " | sed s/+/*10^/ | bc")
        end repeat
        set item i of xxx to temp
    end if
end repeat
return xxx

ASObjC Runnerには、リストを操作するための優れたツールもいくつか用意されています。

于 2013-02-20T05:46:42.010 に答える
-2

RubyまたはPython(または他のほとんどすべての言語)を使用する方が簡単です。

"1,2,3+11,4,5,6
6,5,4,3,2,1+17".split("\n").each { |n|
  n.gsub!(/\+(\d+)/, "0" * $1.to_i)
  c = n.split(",")
  c[4] = c[4].to_i * 5
  c.delete_at(1)
  puts c.join(",")
}
于 2013-02-19T19:53:02.160 に答える