【问题标题】:Parse text with Applscripts使用 Applscripts 解析文本
【发布时间】:2016-09-20 14:27:39
【问题描述】:

我可以使用一些帮助编写 Applescript 来解析 .txt 文件中的一些文本并输出到单独的 .txt 文件。我需要一个脚本来 1)识别一个部分(由“»=”删除),2)识别一个部分中是否存在一组特定的字符,“[*]”,3)打印包含特定部分的所有部分的部分标题字符集,4) 重复到文件末尾。基本上,这些是带有操作项的注释,我想提取包含打开的操作项的注释。

notes.txt中输入的文本结构如下:

»===============================

<Date> 2016-09-19
<Topic> The topic of the day
<Attendees> Mario, Luigi

»1) Where to go from here
- Someone said this
    [*] Mario: schedule this meeting
- And who wants to do that other thing?

»2) And on to the next thing

»3) So on and so forth [*] Luigi can order the pizza
»===============================

<Date> 2016-09-18
<Topic> The topic of the yesterday
<Attendees> Zelda, Wario, Snoop Dog

»1) Where we went from there
- Who said this
    [x] Mario: scheduled this meeting yesterday
- And who wants to do that other thing?

»2) And on to the next thing again

»3) So on and so forth * Luigi can order the sausages
»===============================

actionItems.txt 的输出文本如下:

»===============================

<Date> 2016-09-19
<Topic> The topic of the day
<Attendees> Mario, Luigi

»1) Where to go from here
- Someone said this
    [*] Mario: schedule this meeting
- And who wants to do that other thing?

»3) So on and so forth [*] Luigi can order the pizza
»===============================

也许我可以得到一些帮助?谢谢!

【问题讨论】:

    标签: applescript


    【解决方案1】:

    使用 Applescript 读取和写入文本文件非常容易:

    阅读:

    set myFile to choose file "Select your txt file" -- ask user to select txt file
    set myText to (read myFile) as string -- content the text file is in variable myText
    

    将文本变量newText的内容写入桌面上的文件'action items.txt':

    set newFile to ((path to desktop) as string) & "actionItems.txt"
    try
        open for access file newFile with write permission
        write (newText & return) to file newFile starting at eof
        close access file newFile
    on error
    try
        close access file newFile
    end try
    end try
    

    在这两者之间,您必须使用强大的“项目文本分隔符”来拆分文本。您必须首先定义分隔符,例如:

    set AppleScript's text item delimiters to {"»=", "¬ª="}
    

    (在我的测试中,我发现你的“»=”实际上应该设置为“¬ª=”!!)

    然后您可以通过重复循环访问每个文本项(两个分隔符之间的文本部分):

    set AppleScript's text item delimiters to {"»=", "¬ª="}
    set myNotes to text items of myText
    set newText to ""
    repeat with I from 1 to (count of myNotes) --loop through each item
        -- do what ever you need with 'item I of myNotes'
        set Newtext to "this is my test" -- assign new text to be written
    end repeat
    

    您需要在循环中调整测试:例如,如果您的文本以空格开头,那么第一项将不是您预期的标题,而是空格!

    最后一件事:要测试文本变量是否包含某些内容,请使用“包含”一词:

    Set A to "This is my text sample"
    if A contains "is my" then 
        -- A contains 'is my'
    else
        -- A do not contains 'is my'
    end if
    

    祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-05
      • 1970-01-01
      • 1970-01-01
      • 2012-08-09
      • 2018-10-27
      • 2012-12-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多