gpt4 book ai didi

random - Applescript:修剪空格和回车线

转载 作者:行者123 更新时间:2023-12-02 07:43:25 25 4
gpt4 key购买 nike

我写了一个 AppleScript,它从一个用逗号分隔的文本文件中返回一个随机字符串:

set some_file to "Macintosh HD:Users:Zade:Library:Application Support:Notational Data:Words.txt" as alias
set the_text to read some_file as string
set the text item delimiters of AppleScript to ", "
set the_lines to (every text item of the_text)
return some item of the_lines

但我现在有一个由换行符而不是逗号分隔的文本文件。每行以 0-20 个空格开头。我只想返回随机行的文本部分。我该怎么做?

另外,如果文本部分被普通(不是 curl )引号包围,我该如何修剪引号?

最佳答案

以下脚本将处理您的注释中的附加要求,并包含用于修剪字符串开头和结尾字符的语句。它会排除从文件中读取的前 27 行,并会反复从列表中获取和修剪随机行,直到最终结果不为空。

set theFile to (choose file)
set theLines to paragraphs 28 thru -1 of (read theFile)

repeat -- forever

set someLine to some item of theLines

-- trim characters at the start
if someLine is not "" then repeat until the first character of someLine is not in {space, tab, quote}
if (count someLine) is 1 then
set someLine to ""
exit repeat
end if
set someLine to text 2 thru -1 of someLine
end repeat

-- trim characters at the end
if someLine is not "" then repeat until the last character of someLine is not in {quote}
if (count someLine) is 1 then
set someLine to ""
exit repeat
end if
set someLine to text 1 thru -2 of someLine
end repeat

if someLine is not "" then exit repeat
end repeat

return someLine

关于random - Applescript:修剪空格和回车线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8829824/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com