This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

CSV format
#1
Hi, 
I use this script for sending log data to csv file.
The script make one new row for each value, but i nedd to have them in one row. how can i make this?

ids = {
  ['1'] = 'GENOMSNITTLIG VINDHASTIGHET',
  ['2'] = 'RELATIV LUFTFUKTIGHET',
}

require('trends')

dates = {}
dates['start'] = os.date('*t')
dates['start'].day = dates['start'].day - 1
dates['end'] = os.date('*t')

buf = {}
now = os.time()

for id, tname in pairs(ids) do
  values = trends.fetch(tname, dates)
  count = #values
  step = 86400 / count

  for i, value in ipairs(values) do
    buf[ #buf + 1 ] = {
      now - (count - i) * step,
      id,
      value
    }
  end
end

table.sort(buf, function(a, b)
  return a[ 1 ] > b[ 1 ]
end)

for i, row in ipairs(buf) do
  row[ 1 ] = os.date('%Y-%m-%d %H:%M', row[ 1 ])
  buf[ i ] = table.concat(row, ',')
end

csv = table.concat(buf, '\n')

to = 'email@dot.com '

settings = {
  -- "from" field, only e-mail must be specified here
  from = 'email@dot.com',
  -- smtp username
  user = ' email@dot.com ',
  -- smtp password
  password = '****',
  -- smtp server
  server = 'smtp.gmail.com',
  -- smtp server port
  port = 465,
  -- enable ssl, required for gmail smtp
  secure = 'sslv23',
}

subject = 'CSV väder'

smtp = require('socket.smtp')
mime = require('mime')
ltn12 = require('ltn12')

function escape(v)
  return '<' .. tostring(v) .. '>'
end

to = escape(to)

msgt = {
  headers = {
    to = to,
    ['content-type'] = 'text/csv',
    ['content-disposition'] = 'attachment; filename="logs.csv"',
    ['content-transfer-encoding'] = 'BASE64',
    subject = subject,
  },
  body = ltn12.source.chain(
    ltn12.source.string(csv),
    ltn12.filter.chain(mime.encode('base64'), mime.wrap('base64'))
  )
}

settings.source = smtp.message(msgt)
settings.from = escape(settings.from)
settings.rcpt = { to }

res, err = smtp.send(settings)
log(res, err)


This is the way WOW want the file for weather data shall aranged.

Id,Site Id,Site Authentication Key,Report Date / Time,Concrete Temp.,Day of Gales,Soil Temp. (at 10cm),Wet Bulb,Soil Temp. (at 30cm),Max. Temp. (last 24hr),Total Cloud Cover,Wind Gust,Day of Hail,Wind Gust Direction,Present Weather,Ground State,Soil Temp. (at 100cm),Grass Temp.,Sunshine,Day of Snow,Mean Sea-Level Pressure,Pressure (At Station),Relative Humidity ,Weather Diary,Rainfall Accumulation,Visibility,Min. Temp. (last 24hr),Wind Direction,Wind Speed,Air Temperature,Snow Depth,Soil Moisture,Dew Point,Day of Thunder,Rainfall Rate,Rainfall,Travel Disruption,Hazards causing Travel Disruption,Property or Infrastructure Damage,Hazards causing Property or Infrastructure Damage,Personal Health and Safety,Hazards causing Personal Health and Safety,Utility Disruption,Hazards causing Utility Disruption,Service or Business Disruption,Hazards causing Service or Business Disruption,Agriculture Habitat Damage,Hazards causing Agriculture Habitat Damage,Disruption to Camping Events Leisure Activities,Hazards causing Disruption to Camping Events Leisure Activities

Can be like this...

,,,15/06/2012 15:00,,,,,,,,,,202,100 - Cloudless sky (day),,,,1,,1017,,53,,0,7,,202,6.9,24.5,0,,14.2,,,,,,,,,,,,,,,,,
Reply


Messages In This Thread
CSV format - by jobb@nordstrandsel.se - 13.09.2023, 15:52
RE: CSV format - by admin - 14.09.2023, 07:21
RE: CSV format - by jobb@nordstrandsel.se - 14.09.2023, 09:41
RE: CSV format - by admin - 14.09.2023, 10:09
RE: CSV format - by jobb@nordstrandsel.se - 14.09.2023, 11:41
RE: CSV format - by admin - 14.09.2023, 11:47
RE: CSV format - by jobb@nordstrandsel.se - 14.09.2023, 12:06
RE: CSV format - by admin - 14.09.2023, 12:36
RE: CSV format - by jobb@nordstrandsel.se - 14.09.2023, 13:23

Forum Jump: