我对编写代码非常陌生。我一直在研究如何在文本文档中找到一个字符串,然后在下面的行中返回字符串的一部分。理想情况下,最终目标是将提取出来的字符串放入excel文件中,但我还没有接近这一步。我一直在玩很多不同的选择,我不能为我的生活去工作。我觉得我离得很近,这让我很难受,因为我不知道自己在哪里出了问题。
目标:从下面的文本中提取发布职务的人的姓名,而不知道此人的姓名。我知道字符串“职务张贴”将立即预置我正在寻找的名称,我知道“·”将立即跟随名称。不,文本文档中的任何一个环绕字符串都会出现。
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"到目前为止,我的尝试如下(我的问题是,它似乎只是返回整个文本文档,而不是我正在寻找的名称)。
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
发布于 2016-09-05 14:58:10
我只是很难确定你的第二个分离器到底是什么。您的文本示例显示了“·”,但是当我检查“Elberg”之后和“第二……”之前的内容时,我发现了4个字符:代码32 (空格)、代码194 (N)、代码183 (∑)、代码32 (空格)。
在下面的脚本中,我使用了代码194。当我将您的文本示例剪切/粘贴到文件中时,它会工作。下面是脚本:
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是'‘。
发布于 2016-09-05 16:17:04
您使用AppleScript's text item delimiters的想法是正确的,但是您试图提取名称的方式给您带来了麻烦。不过,首先,我将介绍一些您可以做的改进脚本的事情:
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可以对整个段落或更多段落进行操作。
删除这些不必要的步骤(并添加新的步骤以使其在整个文件上工作)会大大缩小脚本:
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这里给出的输出是错误的:
set last_word to last text item of latter_part
set output to output & ("$ " & last_word as string)这是错误的。这不是你想要的最后一个字;那是文件的最后一个字!若要提取职务列表的海报,请将其更改为:
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 ""即可。
所以,您的最终工作脚本如下所示:
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 outputhttps://stackoverflow.com/questions/39323306
复制相似问题