问题描述
限时送ChatGPT账号..这个问题是在我之前的问题How将 CSV 转换为多个列表 (applescript)?
This question follows on from my previous question How to transform a CSV into several lists (applescript)?
我有一个大约 4000 行的 csv 文件.我将其导入并将每一行作为列表的一个项目,然后将每一行作为自己的项目列表.
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.
例如:(我使用的是数字,但我拥有的数据也包含文本,并且大多数条目都超过 1 个字符,有些条目也是空白的)
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
变成
{"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"}}
现在我想做的是:
从每个列表中删除某些项目,例如我想从每个列表中删除第二个项目,即2"、5"、8"和4"
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"
对某些项目运行计算,例如我想将项目 5 乘以 2
Run calculations on certain items, so for example I want to multiply item 5 by 2
此外,我的数据中的一些数字末尾有 +11 或 +17,我想知道如何将其替换为匹配数量的零,例如,如果我有 5002+6 我想把它变成 5002000000
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
当前代码是:
-- 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.
推荐答案
如果你自己编写一个子程序来为你做这些事情,那真的并不难.这是您要求的 3 个.您需要添加其他子例程,以便您可以执行其他数学运算(加、减和除).只需复制multiplyItemNumberByValue() 子例程并将运算符更改为适当的操作符,它应该无需进一步更改即可工作.
It's really not hard if you write yourself a subroutine to do the stuff for you. Here's the 3 you requested. You'll need to add other subroutines so you can do other math operations (add, subtract, and divide). Just duplicate the multiplyItemNumberByValue() subroutine and change the operator to the appropriate one and it should work with no further changes.
祝你好运.
set listOfLists to {{"1", "2+2", "3", "4", "5", "6"}, {"6", "5", "4", "3", "2", "1"}, {"7", "8", "9", "0", "7", "6"}, {"3", "4", "4", "5+3", "3", "1"}}
-- before performing any other operation, expand all zeros
set listOfLists to expandZeros(listOfLists)
-- remove item 2 from every sublist
set listOfLists to removeItemNumber(listOfLists, 2)
-- multiply item 1 of every sublist by 5
set listOfLists to multiplyItemNumberByValue(listOfLists, 1, 5)
(****************** SUBROUTINES ******************)
on multiplyItemNumberByValue(listOfLists, itemNumber, theValue)
set newList to {}
repeat with i from 1 to count of listOfLists
set thisList to item i of listOfLists
set thisItem to item itemNumber of thisList
set newValue to (thisItem as number) * theValue
set item itemNumber of thisList to (newValue as text)
set end of newList to thisList
end repeat
return newList
end multiplyItemNumberByValue
on removeItemNumber(listOfLists, itemNumber)
set newList to {}
repeat with i from 1 to count of listOfLists
set thisList to item i of listOfLists
if itemNumber is equal to 1 then
set newSublist to items 2 thru end of thisList
else if itemNumber is equal to (count of thisList) then
set newSublist to items 1 thru (itemNumber - 1) of thisList
else
set newSublist to items 1 thru (itemNumber - 1) of thisList & items (itemNumber + 1) thru end of thisList
end if
set end of newList to newSublist
end repeat
return newList
end removeItemNumber
on expandZeros(listOfLists)
set newList to {}
repeat with i from 1 to count of listOfLists
set thisList to item i of listOfLists
set newSublist to {}
repeat with j from 1 to count of thisList
set subItem to item j of thisList
if subItem contains "+" then
set x to offset of "+" in subItem
if x is equal to 0 or x is equal to 1 or x is equal to (count of subItem) then
set end of newSublist to subItem -- do nothing
else
set a to text 1 thru (x - 1) of subItem
set b to text (x + 1) thru end of subItem
repeat (b as number) times
set a to a & "0"
end repeat
set end of newSublist to a
end if
else
set end of newSublist to subItem -- do nothing
end if
end repeat
set end of newList to newSublist
end repeat
return newList
end expandZeros
这是其他数学子程序...
Here's the other math subroutines...
on divideItemNumberByValue(listOfLists, itemNumber, theValue)
set newList to {}
repeat with i from 1 to count of listOfLists
set thisList to item i of listOfLists
set thisItem to item itemNumber of thisList
set newValue to (thisItem as number) / theValue
set item itemNumber of thisList to (newValue as text)
set end of newList to thisList
end repeat
return newList
end divideItemNumberByValue
on addValueToItemNumber(listOfLists, itemNumber, theValue)
set newList to {}
repeat with i from 1 to count of listOfLists
set thisList to item i of listOfLists
set thisItem to item itemNumber of thisList
set newValue to (thisItem as number) + theValue
set item itemNumber of thisList to (newValue as text)
set end of newList to thisList
end repeat
return newList
end addValueToItemNumber
on subtractValueFromItemNumber(listOfLists, itemNumber, theValue)
set newList to {}
repeat with i from 1 to count of listOfLists
set thisList to item i of listOfLists
set thisItem to item itemNumber of thisList
set newValue to (thisItem as number) - theValue
set item itemNumber of thisList to (newValue as text)
set end of newList to thisList
end repeat
return newList
end subtractValueFromItemNumber
这篇关于如何读取/修改/删除applescript中列表中包含的项目/列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论