使用AppleScript从CSV查找创建字符串

时间:2014-10-26 21:07:58

标签: csv applescript

我正在尝试创建一个将从CSV文件中创建字符串的AppleScript。创建整个文件的字符串非常容易,但我希望能够扫描某个组织的文件。以下是我的输入和所需输出。

我的CSV:

Org1    Bobby Bob   bobbybob@gmail.com
Org1    Wendy Wen   wendywen@gmail.com
Org1    Rachel Rach rachelrach@gmail.com
Org2    Timmy Tim   ttim@otheremail.com
Org2    Ronny Ron   rron@otheremail.com
Org2    Mike Mik    mmik@otheremail.com

我的AppleScript:

set csv to read csv_input_file as «class utf8»
set text item delimiters to ","
set csvParagraphs to paragraphs of csv

repeat with current_line in paragraphs of csv
    set organization to text item 1 of current_line

    --THIS IS WHERE I NEED HELP
    set others_in_organization to ...

end repeat

期望输出:

我想创建一个新字符串,其中包含当前组织中所有其他人的逗号分隔列表。例如,第四个条目的组织是" Org2"。所以字符串将返回" Timmy Tim,Ronny Ron,Mike Mik"。

任何帮助都会很棒!谢谢!

1 个答案:

答案 0 :(得分:2)

试试这个......

set myOrg to "Org1"
set otherPeople to ""

set csv to "Org1,Bobby Bob,bobbybob@gmail.com
Org1,Wendy Wen,wendywen@gmail.com
Org1,Rachel Rach,rachelrach@gmail.com
Org2,Timmy Tim,ttim@otheremail.com
Org2,Ronny Ron,rron@otheremail.com
Org2,Mike Mik,mmik@otheremail.com"

set text item delimiters to ","
repeat with aLine in paragraphs of csv
    set textItems to text items of aLine
    if item 1 of textItems is not myOrg then
        set otherPeople to otherPeople & item 2 of textItems & ", "
    end if
end repeat
set text item delimiters to ""

if otherPeople ends with ", " then
    set otherPeople to text 1 thru -3 of otherPeople
end if
return otherPeople