This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Glotz659's profile picture

Csvconverter - Convert Tables To Csv-Files (And Vice-Versa)

Started by Glotz659, 07 August 2013 - 07:38 AM
Glotz659 #1
Posted 07 August 2013 - 09:38 AM
This is an api that can convert tables to .csv-files and get tables from .csv-files
the code: pastebin get f0T7fUyG csvConverter


functions:

os.loadAPI("csvConverter")
csvConverter.read(fileName)
-- reads a csv file and stores its content in a table
-- returns table
csvConverter.write(table, fileName)
-- writes content from the given table (must be 2-dimensional)
-- into a csv-file
-- returns nil

example usage:
  • analyze data from experiments (e.g. flight time of icbm-rockets) in excel or openoffice calc
  • get large lists (e.g. prime numbers) into cc without much work
immibis #2
Posted 07 August 2013 - 09:25 PM
Comma Separated Values means the values are separated by commas, not semicolons.
theoriginalbit #3
Posted 07 August 2013 - 09:51 PM
Comma Separated Values means the values are separated by commas, not semicolons.
OP could mean Character Separated Values, not Comma Separated Values… CSV files can be either…
UMayBleed #4
Posted 08 August 2013 - 10:11 AM
I got a suggestion, that you may want to use.
the pairs for loop, like

for i,v in pairs(tLines) do
  sLine = textutils.serialize(v)
  sLine = sLine:sub(2, #sLine-1) --this removes "{" and "}"
  h.writeLine(string.gsub(sLine, "%[(%w+)]=(%x+),", "%2;"))
end

Because what if i had a table like so:

myTable = {}
myTable[1] = "Hey!"
myTable[3] = "Hey!"
myTable[4] = "Hey!"
myTable[7] = "Hey!"
immibis #5
Posted 08 August 2013 - 06:24 PM
According to wikipedia, the general form is called Delimiter Separated Values.
theoriginalbit #6
Posted 10 August 2013 - 04:22 AM
According to wikipedia, the general form is called Delimiter Separated Values.
Well if we're going to use wikipedia….

A comma-separated values (CSV) (also sometimes called character-separated values, because the separator character does not have to be a comma) file stores tabular data (numbers and text) in plain-text form.

http://en.wikipedia....eparated_values
BigTwisty #7
Posted 06 September 2013 - 08:14 AM
I got a suggestion, that you may want to use.
the pairs for loop, like

for i,v in pairs(tLines) do
  sLine = textutils.serialize(v)
  sLine = sLine:sub(2, #sLine-1) --this removes "{" and "}"
  h.writeLine(string.gsub(sLine, "%[(%w+)]=(%x+),", "%2;"))
end

Because what if i had a table like so:

myTable = {}
myTable[1] = "Hey!"
myTable[3] = "Hey!"
myTable[4] = "Hey!"
myTable[7] = "Hey!"

Unfortunately pairs() does not always return values in order. You would have to add some sort functionality as well.
theoriginalbit #8
Posted 06 September 2013 - 08:44 AM
I got a suggestion, that you may want to use.
the pairs for loop, like

for i,v in pairs(tLines) do
  sLine = textutils.serialize(v)
  sLine = sLine:sub(2, #sLine-1) --this removes "{" and "}"
  h.writeLine(string.gsub(sLine, "%[(%w+)]=(%x+),", "%2;"))
end

Because what if i had a table like so:

myTable = {}
myTable[1] = "Hey!"
myTable[3] = "Hey!"
myTable[4] = "Hey!"
myTable[7] = "Hey!"

Unfortunately pairs() does not always return values in order. You would have to add some sort functionality as well.
It does always return indexed values in order though. It's just the key/value pairs that it does not return in any particular order.

However, ask and you shall receive. :P/>

Commented
Spoiler

local function orderedPairs(_t)
  --# define a table to store the keys in
  local tKeys = {}

  --# store the keys from the original table into the new keys table
  for k in pairs(_t) do
	table.insert(tKeys, k)
  end

  --# sort the table with a custom function
  table.sort(tKeys, function( a,b )
	--# we convert to strings here so that it can sort indexed and key'd values with each other, in Lua we can use comparison operators such as < and > on strings and it compares the bytes of each character
	return tostring(a) < tostring( b )
  end)

  --# the index to return
  local nAt = 0

  return function()
	--# increment the index
	nAt = nAt + 1
	--# grab the key for the current index
	local k = tKeys[nAt]
	--# return the key and the value from the original table
	return k, _t[k]
  end
end

Uncommented
Spoiler


local function orderedPairs(_t)
  local tKeys = {}
  for k in pairs(_t) do
	table.insert(tKeys, k)
  end
  table.sort(tKeys, function( a,b )
	return tostring(a) < tostring( b )
  end)
  local nAt = 0
  return function()
	nAt = nAt + 1
	local k = tKeys[nAt]
	return k, _t[k]
  end
end

The above code also has the added bonus of fixing the "cannot get next of nil" error when attempting to assign nil to table elements in a generic-for loop. :)/>
Edited on 06 September 2013 - 06:46 AM