2012-11-22 36 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»來閱讀。