2012-11-22 32 views
0

我有一个在MacRoman中编码的文本列表,以换行符分隔。不知何故第二个列表无法保存在MacRoman中,所以我不得不使用Unicode UTF-16来获取德语“ö”,“ä”和其他东西。虽然ListA得到了预期的填充,但listB并没有被打破,我最终得到一个单一的字符串,我无法再打破/不知道如何。有人可以帮我吗?在Applescript中通过分隔符打破UTF-16 Unicode文本?

set ListA to (read file myFile1 using delimiter linefeed) as list  
display dialog "" & item 1 of ListA  
--> "Name A" 

set ListB to (read file myFile2 using delimiter linefeed as Unicode text) as list  
display dialog "" & item 1 of ListB  
--> "Name A  
Name B  
Name C  
Name D" 

回答

1

可以有许多不同类型的字符分隔文本文件中的行。这并不总是一个换行。处理它们的最简单方法是使用applescript命令“段落”,而不是在读取文件时使用分隔符。段落在确定使用什么字符并处理它时非常好。它并不总是有效,但在深入研究这个问题之前,这是值得一试的。因此,尝试阅读你的文件,像这样...

set ListB to paragraphs of (read file myFile2 as Unicode text) 

如果这样不行,那么你将不得不尝试弄清楚这个角色是什么。我在这些情况下所做的是在物理上打开文件并使用我的鼠标选择返回字符并复制它。然后我回到AppleScript Editor并将其粘贴到此命令中。把它粘贴在我有字母“a”的地方。它会给你的角色ID。

id of "a" 

然后你可以使用这样的分隔符,显然是在利用上述代替97从命令ID号读取文件...

set ListB to read file myFile2 using delimiter (character id 97) as Unicode text 
0

确定的文件使用LF行结局?这适用于我:

set f to POSIX file "/tmp/1" 
set b to open for access f with write permission 
set eof b to 0 
write "あ" & linefeed & "い" to b as Unicode text -- UTF-16 
close access b 
read f using delimiter linefeed as Unicode text 

您是否尝试将文件保存为UTF-8?您可以通过将Unicode text替换为«class utf8»来阅读。