2017-06-04 136 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»)