代码之家  ›  专栏  ›  技术社区  ›  Duck

applescript:转换列表中的txt文件

  •  1
  • Duck  · 技术社区  · 15 年前

    我有一个文本格式的文件,它有一个数字列表。每行一个数字,例如:

    978-972-722-649-8
    978-972-722-646-7
    978-972-722-627-6
    978-972-722-625-2
    978-972-722-594-1
    

    等。

    此列表位于一个名为file.txt的文件上,它是使用西方(ISO拉丁语1)编码创建并保存在textedit中的。

    现在我尝试使用applescript来读取这个文件并在列表中转换它。

    我有这个剧本

    set myDirectory to path to desktop folder as string
    set myFile to myDirectory & "file.txt"
    
    try
        set open_file to ¬
            open for access file myFile without write permission
        set myList to read open_file using delimiter return
        close access open_file
    on error
        try
            close access file open_file
        end try
    end try
    

    运行这个脚本之后,mylist只包含一个条目,这个条目是“97”(我想这是第一个条目的前两个数字)。

    怎么回事?

    谢谢。

    3 回复  |  直到 7 年前
        1
  •  1
  •   jenmbq stib    7 年前

    听起来这个文件有点奇怪,但是你尝试过其他方法吗?

    set myfilePPath to posix path of myFile
    set myList to every paragraph of (do shell script("cat \"" & myfilePPath & "\""))
    

    这个 程序只是输入文件的内容。

        2
  •  2
  •   Vlad    15 年前

    如果您将文件保存为western(iso-latin 1),则很可能它有LF作为分隔符。

    尝试使用以下行:

    将mylist设置为使用分隔符读取打开的文件\n

    看看是否适合你。

    我试着在我的笔记本电脑上运行你的脚本,它在上面的修改下工作得很好。

    另外,在shell中运行od命令,查看文件是否正确编码。以下是我的命令的输出:

    vlad$ od -c file.txt 
    0000000    9   7   8   -   9   7   2   -   7   2   2   -   6   4   9   -
    0000020    8  \n   9   7   8   -   9   7   2   -   7   2   2   -   6   4
    0000040    6   -   7  \n   9   7   8   -   9   7   2   -   7   2   2   -
    0000060    6   2   7   -   6  \n   9   7   8   -   9   7   2   -   7   2
    0000100    2   -   6   2   5   -   2  \n   9   7   8   -   9   7   2   -
    0000120    7   2   2   -   5   9   4   -   1  \n                        
    0000132
    
        3
  •  1
  •   has    15 年前

    这个 read 命令 using delimiter(s) 参数有问题。请使用以下选项,该选项适用于任何换行符:

    set myList to every paragraph of (read open_file)