2012-04-10 91 views
2

我有一个从CSV读取数据的applescript(成功!)。在我的脚本完成编辑数据后,我希望它将修改后的文本写回该文件。 当我的文本被写回到CSV文件时,它已经失去了所有的回车,所以它读取为一行。我做错了什么?使用Applescript将列表写入CSV

set theFile to choose file with prompt "Select a text file:" 
set theFileContents to read theFile 
... 


set theList to paragraphs of theFileContents 



repeat with i from 2 to count theList 

set AppleScript's text item delimiters to "," 
set theLine to theList's item i 
theLine 
set clinic_id to first text item of theLine 
.... 
set AppleScript's text item delimiters to "" 

... 

    open for access theFile with write permission 

    set theData to theList as text 
    write the [theData] to theFile as text 
    close access theFile 


    display dialog the [theLine] 
end if 
end repeat 

有什么建议吗?这段代码的输出是:

1,2,3,A,B,C,X,Y,Z 

当我要的是

1,2,3 
A,B,C 
X,Y,Z 
+0

如果我没有弄错,'theList'变量将原始文件的所有段落(行)保存为一个列表,因此将它写入循环*内的文件*将写入文件的*整个内容*尽可能多的你有线。在'end repeat' *之后写入文件*可能更有意义? – fanaugen 2012-04-11 07:58:26

回答

3

我想你theList有行(每行用逗号的字符串)作为其项目,所以它看起来像{"1,2,3", "A,B,C", "X,Y,Z"},对?因此,以便将其写入到文件列表转换为text,设置text item delimiters到换行符:

set AppleScript's text item delimiters to "\n" 
set theData to {"1,2,3", "A,B,C", "X,Y,Z"} as text 

theData目前拥有价值

"1,2,3 
A,B,C 
X,Y,Z" 

这正是你会在输出文件中找到什么。