我已经研究了一段时间,但找不到解决方案。我有一个 CSV 文件,其中包含以下数据:
1,2,3,a,b,c,4,5,6,d,e,f,7,8,9,0
0,9,8,g,h,i,7,6,4,k,l,m,5,4,3,2
等等...
除了某些为真或为假的字段外,每一行都包含大部分不同的数据。
我需要的是将每一行作为列表拉出,然后能够解析数据。
我希望能够从所有列表中删除项目编号 3,然后保存要在电子表格中使用的数据。
请记住,我对 applescript 比较陌生,所以仍在尝试掌握它,已经购买了一本在许多网站上强烈推荐的书,但它还没有交付! :-)
此外,如果您知道一种不同/更好的方法,那么我很乐意尝试。
目前我有以下内容:
Blockquote -- Setting Variables
set LF to ASCII character 10
-- Choosing your files
set prompt1 to "Please locate your Devices CSV file..."
set csvDevices to "testfile.csv"
--set csvDevices to (choose file with prompt prompt1)
-- Reading file to memory
set csvLines to read csvDevices
set AppleScript's text item delimiters to {LF}
set csvEntries to paragraphs of csvLines
set AppleScript's text item delimiters to {","}
-- Filtering rubbish
set csvNoTitle to rest of csvEntries -- Remove Title Line
set lineCount to count csvNoTitle
之后我可以打印出内容,但它会给我
{"1,2,3,a,b,c,4,5,6,d,e,f,7,8,9,0","0,9,8,g,h,i ,7,6,4,k,l,m,5,4,3,2","等..."}
而不是我想要的
{"1","2","3","a","b","c","4","5","6","d","e","f ","7","8","9","0"}
{"0","9","8","g","h","i","7","6","4","k","l","m","5","4","3","2"}
{“等等...”}
顺便说一下,我删除了脚本中的第一行,因为它是没有实际有用数据的描述行,或者无论如何对我来说都不是。
感谢您的帮助
最佳答案
您应该将文本 CSV 文件分解为记录(单行),然后将每个记录分解为值(以逗号分隔)。您可以从将 CSV 文本文件转换为字符串 (csvLines) 开始按照这种方式进行。
-- getting records (you don't need to set applescript's text item delimiters here)
set recs to paragraphs of csvLines
-- getting values for each record
set vals to {}
set applescript's text item delimiters to ","
repeat with i from 1 to length of recs
set end of vals to text items of (item i of recs)
end repeat
-- this is true
(item 1 of vals) = {"1","2","3","a","b","c","4","5","6","d","e","f","7","8","9","0"}
这将解析整个文件而不进行过滤。但只是给出正确的 CSV 解析的想法。您已经知道如何过滤数据。
关于macos - 如何将 CSV 转换为多个列表(applescript)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14933145/