RosettaCodeData/Task/CSV-data-manipulation/Lingo/csv-data-manipulation-2.lingo

45 lines
862 B
Plaintext

sep = ","
eol = numtochar(10)
-- load CSV string from file
fn = _movie.path & "file.csv"
fp = xtra("fileIO").new()
fp.openFile(fn, 1)
csvStr = fp.readFile()
fp.closeFile()
-- parse CSV string into propList
csvData = parseSimpleCSVString(csvStr, sep, eol)
-- add SUM column
csvData[#header].append("SUM")
repeat with row in csvData[#data]
sum = 0
repeat with cell in row
sum = sum+integer(cell)
end repeat
row.append(sum)
end repeat
-- create CSV string from updated propList
csvString = createSimpleCSVString(csvData, sep, eol)
-- save CSV string to file
fn = _movie.path & "file.csv"
fp.openFile(fn, 2)
if not fp.status() then fp.delete()
fp.createFile(fn)
fp.openFile(fn, 2)
fp.writeString(csvString)
fp.closeFile()
-- show the CSV string
put csvString
-- "C1,C2,C3,C4,C5,SUM
1,5,9,13,17,45
2,6,10,14,18,50
3,7,11,15,19,55
4,8,12,16,20,60
"