如何读取/修改/删除包含在AppleScript的列表中的项目/名单 [英] How to read/modify/remove items/lists contained in a list in applescript
问题描述
这个问题从我的previous问题How来转换CSV成几个列表(AppleScript的)?
我有一个约4000行的csv文件。我将其导入,使每一行放入一个列表中的项目,然后我做每一行是项自己的名单。
例如:(我用的电话号码,但我有数据包含文本以及大部分条目超过1字符,有些项也是空白)
1,2,3,4,5,6
6,5,4,3,2,1
7,8,9,0,7,6
3,4,4,5,3,1
变为
{1,2,3,4,5,6,6,5,4,3,2,1,7,8,9,0, 7,6,3,4,4,5,3,1}
变为
<$p$p><$c$c>{{\"1\",\"2\",\"3\",\"4\",\"5\",\"6\"}{\"6\",\"5\",\"4\",\"3\",\"2\",\"1\"}{\"7\",\"8\",\"9\",\"0\",\"7\",\"6\"}{\"3\",\"4\",\"4\",\"5\",\"3\",\"1\"}}我现在想什么,能够做的是以下内容:
从每个列表中删除某些项目,例如,我想从每个列表中删除的第二个项目,这是2,5,8和4
在某些项目运行计算,所以比如我要乘以2 5项
另外一些我的数据的数字有+11或+ 17在他们的最后,我想知道如何更换零的匹配量,因此,例如,如果我有5002 + 6我想使之变成50.02亿
当前code是:
- 选择您的文件
设置csvDevices为testfile.csv - 读文件到内存中
设置csvDevices阅读csvDevices - 创建记录(单行)
设置csvDevicesRecords为csvDevices段落 - 删除标题行
设置csvDevicesRecords为csvDevicesRecords休息 - 使每行到一个列表
集csvDevicesValues到{}
设置的AppleScript的文本项分隔符,
与我重复1到csvDevicesRecords的长度
csvDevicesValues的设置结束文本的项目(项目我csvDevicesRecords的)
重复结束
设置的AppleScript的文本项分隔符为
我希望以上的都有道理。
奈杰尔·加维的CSV-到列表转换是我见过的最好的。
您可以调用它是这样的:
设置文件路径为/Users/You/Desktop/myCsv.csv
设置csvData做shell脚本猫和安培;文件路径的引号形式
设置csvDevicesLists为csvToList(csvData,{微调:真}) - &GT; {{1,2,3,4,5,6},{6,5,4,3,2,1 },{7,8,9,0,7,6},{3,4,4,5,3, 1}}
您可以垫零是这样的:
设置XXX为{{1,2,3 + 2,4,52 + 3,6},{6 ,5,4 + 5,3,2,1}}
与我再说一遍,从1数的XXX
如果(XXX为文本的我的项目)中含有+,那么
SET TEMP为{}
与anItem重复项我的XXX
SET TEMP结束(做shell脚本回声&放大器; anItem&放引号形式;| sed的S / + / * 10 ^ / | BC)
重复结束
设置项目XXX到temp中的我
万一
重复结束
返回XXX
ASObjC亚军提供了一些很好的工具来操作的清单。
This question follows on from my previous question How to transform a CSV into several lists (applescript)?
I have a csv file with about 4000 lines. I import it and make each line into an item of a list, I then make each line it's own list of items.
eg: (I am using numbers but the data I have contains text as well and most of the entries are more than 1 char, some entries are also blank)
1,2,3,4,5,6
6,5,4,3,2,1
7,8,9,0,7,6
3,4,4,5,3,1
becomes
{"1,2,3,4,5,6","6,5,4,3,2,1","7,8,9,0,7,6","3,4,4,5,3,1"}
becomes
{{"1","2","3","4","5","6"}{"6","5","4","3","2","1"}{"7","8","9","0","7","6"}{"3","4","4","5","3","1"}}
now what I would like to be able to do is the following:
Delete certain items from each list, so for example I want to delete the second Item from Each list, that's "2", "5", "8" and "4"
Run calculations on certain items, so for example I want to multiply item 5 by 2
Also some of the numbers in my data have the +11 or + 17 at the end of them, I would like to know how to replace that with the matching amount of zeros, so for example if i had 5002+6 I would want to make it into 5002000000
Current code is:
-- Choosing your file
set csvDevices to "testfile.csv"
-- Reading file to memory
set csvDevices to read csvDevices
-- Creating Records (Single Lines)
set csvDevicesRecords to paragraphs of csvDevices
-- Remove Title Line
set csvDevicesRecords to rest of csvDevicesRecords
-- Make each line into a list
set csvDevicesValues to {}
set AppleScript's text item delimiters to ","
repeat with i from 1 to length of csvDevicesRecords
set end of csvDevicesValues to text items of (item i of csvDevicesRecords)
end repeat
set AppleScript's text item delimiters to ""
I hope the above all makes sense.
Nigel Garvey's CSV-to-list converter is the best I have seen.
You can call it like this:
set filePath to "/Users/You/Desktop/myCsv.csv"
set csvData to do shell script "cat " & quoted form of filePath
set csvDevicesLists to csvToList(csvData, {trimming:true})
--> {{"1", "2", "3", "4", "5", "6"}, {"6", "5", "4", "3", "2", "1"}, {"7", "8", "9", "0", "7", "6"}, {"3", "4", "4", "5", "3", "1"}}
You can pad zeros like this:
set xxx to {{"1", "2", "3+2", "4", "52+3", "6"}, {"6", "5", "4+5", "3", "2", "1"}}
repeat with i from 1 to count of xxx
if (item i of xxx as text) contains "+" then
set temp to {}
repeat with anItem in item i of xxx
set end of temp to (do shell script "echo " & quoted form of anItem & " | sed s/+/*10^/ | bc")
end repeat
set item i of xxx to temp
end if
end repeat
return xxx
ASObjC Runner provides some good tools for manipulating lists as well.
这篇关于如何读取/修改/删除包含在AppleScript的列表中的项目/名单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!