Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function read(csvFile)
- if fs.exists(csvFile) then
- local tLines = {}
- local h = fs.open(csvFile, "r")
- local lineCounter = 1
- while true do
- sLine = h.readLine()
- if not sLine then -- we reached the end of file
- break
- else
- tLines[lineCounter] = textutils.unserialize("{"..sLine.."}")
- end
- lineCounter = lineCounter+1
- end
- -- swap x and y in tLines and return
- local helperRows = #tLines[1]
- local helperCols = #tLines
- local tReturn = {}
- for i = 1, helperRows do
- tReturn[i] = {}
- for j = 1, helperCols do
- tReturn[i][j] = tLines[j][i]
- end
- end
- return tReturn
- else
- error("Could not find "..csvFile)
- end
- end
- function write(tInput, csvFile)
- local tLines = {}
- -- swap x and y again
- local helperRows = #tInput[1]
- local helperCols = #tInput
- for i = 1, helperRows do
- tLines[i] = {}
- for j = 1, helperCols do
- tLines[i][j] = tInput[j][i]
- end
- end
- local h = fs.open(csvFile, "w")
- for i = 1, #tLines do
- -- format from "{[1]=value1,[2]=value2}" to "value1;value2;" and write to file
- sLine = textutils.serialize(tLines[i])
- sLine = sLine:sub(2, #sLine-1) --this removes "{" and "}"
- h.writeLine(string.gsub(sLine, "%[(%w+)]=(%x+),", "%2;"))
- -- this removes "[key]=", replaces "," with ";" and writes the line
- end
- h.close()
- end
Advertisement
Add Comment
Please, Sign In to add comment