首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用AppleScript从文本文档中提取两个字符串之间的字符串

使用AppleScript从文本文档中提取两个字符串之间的字符串
EN

Stack Overflow用户
提问于 2016-09-05 02:51:20
回答 2查看 2.3K关注 0票数 2

我对编写代码非常陌生。我一直在研究如何在文本文档中找到一个字符串,然后在下面的行中返回字符串的一部分。理想情况下,最终目标是将提取出来的字符串放入excel文件中,但我还没有接近这一步。我一直在玩很多不同的选择,我不能为我的生活去工作。我觉得我离得很近,这让我很难受,因为我不知道自己在哪里出了问题。

目标:从下面的文本中提取发布职务的人的姓名,而不知道此人的姓名。我知道字符串“职务张贴”将立即预置我正在寻找的名称,我知道“·”将立即跟随名称。不,文本文档中的任何一个环绕字符串都会出现。

代码语言:javascript
复制
I'm running OS X El Capitan
file name for this example is ExtractedTextOutput.txt
file location for this example is "/Users/RaquelBianca/Desktop/ExtractTextOutput2.txt"

到目前为止,我的尝试如下(我的问题是,它似乎只是返回整个文本文档,而不是我正在寻找的名称)。

代码语言:javascript
复制
set theFile to ("/Users/RaquelBianca/Desktop/ExtractTextOutput2.txt")
set theFileContents to read theFile

set output to {}
set od to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"
"}

set all_lines to every text item of theFileContents
repeat with the_line in all_lines
if "Job posted by" is not in the_line then
    set output to output & the_line
else
    set AppleScript's text item delimiters to {"Job posted by"}
    set latter_part to last text item of the_line
    set AppleScript's text item delimiters to {" "}
    set last_word to last text item of latter_part
    set output to output & ("$ " & last_word as string)
end if
end repeat

set AppleScript's text item delimiters to {"
"}

set output to output as string
set AppleScript's text item delimiters to od
return output

任何和所有的帮助和想法都是非常感谢的。

文件中的示例文本: 9/2/2016大纽约市地区Datadog的应用程序安全工程师职务LinkedIn 60家庭配置文件职务描述我的网络职务搜索人员、工作、公司等。高级商务服务前往Lynda.c应用程序安全工程师Datadog大纽约市地区15天前发布93次查看,1份明矾作品在公司网站上申请,我们的使命是为云业务带来理智,我们需要您在我们的平台上构建具有弹性和安全的应用程序。您要做的是执行代码和设计评审,贡献代码以提高整个Datadog产品的安全性,教育您的工程师同事了解代码和基础设施监视器用于异常活动的生产应用程序的安全性,并跟踪整个公司的应用程序安全问题,帮助改进我们的安全策略和流程- Ryan Elberg·Datadog大纽约市地区技术人才收购的第二任主管发送Inmail

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-09-05 14:58:10

我只是很难确定你的第二个分离器到底是什么。您的文本示例显示了“·”,但是当我检查“Elberg”之后和“第二……”之前的内容时,我发现了4个字符:代码32 (空格)、代码194 (N)、代码183 (∑)、代码32 (空格)。

在下面的脚本中,我使用了代码194。当我将您的文本示例剪切/粘贴到文件中时,它会工作。下面是脚本:

代码语言:javascript
复制
set theFile to ("/Users/RaquelBianca/Desktop/ExtractTextOutput2.txt")
-- your separator seems to be code 32 (space), code 194 (¬), code 183 (∑), code 32 (space)
set Separator to ASCII character 194 -- is it correct ?

set theFileContents to read theFile
set myAuthor to ""
set AppleScript's text item delimiters to {"Job posted by "}
if (count of text item of theFileContents) is 2 then
set Part2 to text item 2 of theFileContents -- this part starts just after "Job posted by "
set AppleScript's text item delimiters to {Separator}
set myAuthor to text item 1 of Part2
end if

log "result=//" & myAuthor & "//" -- show the result in variable myAuthor

注意:如果文本不包含“职务张贴",那么myAuthor是'‘。

票数 2
EN

Stack Overflow用户

发布于 2016-09-05 16:17:04

您使用AppleScript's text item delimiters的想法是正确的,但是您试图提取名称的方式给您带来了麻烦。不过,首先,我将介绍一些您可以做的改进脚本的事情:

代码语言:javascript
复制
set all_lines to every text item of theFileContents
repeat with the_line in all_lines
    if "Job posted by" is not in the_line then
    set output to output & the_line
else
    …
end repeat

没有必要将文件内容分解为行;如果需要,AppleScript可以对整个段落或更多段落进行操作。

删除这些不必要的步骤(并添加新的步骤以使其在整个文件上工作)会大大缩小脚本:

代码语言:javascript
复制
set theFile to ("/Users/RaquelBianca/Desktop/ExtractTextOutput2.txt")
set theFileContents to read theFile

set output to {}
set od to AppleScript's text item delimiters

if "Job posted by" is in theFileContents
    set AppleScript's text item delimiters to {"Job posted by"}
    set latter_part to last text item of theFileContents
    set AppleScript's text item delimiters to {" "}
    set last_word to last text item of latter_part
    set output to output & ("$ " & last_word as string)
else
    display alert "Poster of job listing not found"
    set output to theFileContents
end if

set AppleScript's text item delimiters to od
return output

这里给出的输出是错误的:

代码语言:javascript
复制
set last_word to last text item of latter_part
set output to output & ("$ " & last_word as string)

这是错误的。这不是你想要的最后一个字;那是文件的最后一个字!若要提取职务列表的海报,请将其更改为:

代码语言:javascript
复制
repeat with theWord in latterPart
    if the first character in theWord is "¬" then exit repeat
    set output to output & theWord
end repeat

由于AppleScript的怪异Unicode处理,无论出于什么原因,在通过脚本运行时,将名称与其他文本分离的点(·)转换为“∑”。所以,我们找“”代替。

一些最后的代码修复:

您的一些变量名使用the_snake_case,而其他变量则使用theCamelCase。使用这样或那样的约定通常是个好主意,所以我也修正了这一点。

我以为你出于任何原因想要输出那个美元标志,所以我把它保存在里面。如果不需要,只需将set output to "$ "替换为set output to ""即可。

所以,您的最终工作脚本如下所示:

代码语言:javascript
复制
set theFile to "/Users/RaquelBianca/Desktop/ExtractTextOutput2.txt"
set theFileContents to read theFile as text

set output to "$ "
set od to AppleScript's text item delimiters

if "Job posted by" is in theFileContents then
    set AppleScript's text item delimiters to {"Job posted by"}
    set latterPart to last text item of theFileContents
    set AppleScript's text item delimiters to {" "}
    repeat with theWord in latterPart
        if the first character in theWord is "¬" then exit repeat
        set output to output & theWord
    end repeat
else
    display alert "Poster of job listing not found"
    set output to theFileContents
end if

set AppleScript's text item delimiters to od
return output
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39323306

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档