2

我有一个从 CSV 读取数据的小程序(成功!)。在我的脚本完成数据编辑后,我希望它将修改后的文本写回该文件。 当我的文本被写回 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
4

1 回答 1

3

我想你theList有行(每行一个带逗号的字符串)作为它的项目,所以它看起来像{"1,2,3", "A,B,C", "X,Y,Z"},对吧?因此,将列表转换为 atext以便将其写入文件之前,请将 设置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"

这正是您将在输出文件中找到的内容。

于 2012-04-11T08:06:00.867 回答