我是编写代码的新手。我一直在寻找在文本文档中找到字符串然后在下一行返回部分字符串的各种方法。理想情况下,最终目标是将此提取的字符串放入excel文件中,但我还没有接近该步骤。我一直在玩很多不同的选择,我不能为我的生活让它工作。我觉得自己很亲密,这让我很伤心,因为我无法弄清楚我在哪里出错了。
目标:从下面的文字中提取发布作业的人的姓名,而不知道该人的姓名。我知道字符串“Job posted by”会立即预先找到我正在寻找的名字,我知道“·”会立即跟随这个名字。否则文本文档中的其他位置会出现这些环绕声字符串中的任何一个。
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产品的安全性让您的同事们了解代码和基础架构的安全性 监视异常活动的生产应用程序 优先考虑并跟踪整个公司的应用程序安全问题 帮助改进我们的安全策略和流程 职位发布者 莱恩埃尔伯格·第二名 Datadog大纽约市区技术人才招聘负责人 发送Inmail
答案 0 :(得分:2)
我很难确定你的第二个分隔符是什么。你的文字示例显示'·',但当我检查'Elberg'之后和'2nd'之前的内容时,我发现了4个字符:代码32(空格),代码194(¬),代码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为''。
答案 1 :(得分:0)
您有正确的想法使用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)
这是不正确的。这不是你想要的 last 这个词;那是文件的最后一个字!要提取作业列表的海报,请将其更改为以下内容:
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 output