2017-06-04 141 views
0

我有一個簡單的代碼將文本文件讀入列表中。它是這種格式的CMYK值列表:00, 100, 64, 33。出於某種原因,輸出將替換具有奇怪字符的空間......「¬†」(返回和匕首?)。Applescript:替換空間的奇怪字符(「¬†」)

所以這個腳本:

set cmykList to {} 
set eachLine to paragraphs of (read POSIX file "/Users/me/Desktop/cmyk.txt") 
repeat with nextLine in eachLine 
    if length of nextLine is greater than 0 then 
     copy (nextLine as text) to the end of cmykList 
    end if 
end repeat 
choose from list cmykList 

回報: 00,¬†100,¬†64,¬†33, 00,¬†00,¬†00,¬†00, 100,¬†72,¬†00, 100,¬†35,¬†00,¬†100

爲什麼這是任何想法,我怎樣才能避免這種情況?

文本文件設置像這樣:

00, 100, 64, 33 
00, 00, 00, 00 
100, 72, 00, 18 
100, 35, 00, 100 
00, 16, 100, 00 
00, 100, 63, 29 
00, 66, 100, 07 
03, 00, 00, 32 
100, 35, 00, 100 
00, 100, 81, 04 
04, 02, 00, 45 
00, 00, 00, 00 
03, 00, 00, 32 
100, 35, 00, 100 

編輯:解決了這個問題做一個查找/替換:

set cmykList to {} 
set eachLine to paragraphs of (read POSIX file "/Users/me/Desktop/cmyk.txt") 
repeat with nextLine in eachLine 
    if length of nextLine is greater than 0 then 
     set theText to (nextLine as text) 
     set AppleScript's text item delimiters to " " 
     set theTextItems to text items of theText 
     set AppleScript's text item delimiters to " " 
     set theText to theTextItems as string 
     set AppleScript's text item delimiters to {""} 
     copy (theText as text) to the end of cmykList 
    end if 
end repeat 
set chooseList to choose from list cmykList 

不過還是很好奇,爲什麼發生這種情況在第一地點。

回答

0

這兩個字符(ASCII 194 160)是Unicode NO-BREAK SPACE字符的UTF-8表示。

您不指定文本文件的來源,但無論它來自何處,都是使用非中斷空格而不是常規空格。正如你發現的那樣,當你讀取文件時,你可以通過用普通空格替換它們來修復問題。

0

你的文件包含UTF8編碼的Unicode文本。默認情況下,標準添加的readwrite命令(愚蠢)使用古老經典的MacOS-時期遺留下來的編碼,所以你需要告訴他們明確地使用UTF8:

set eachLine to paragraphs of (read POSIX file "/Users/me/Desktop/cmyk.txt" as «class utf8»)