Page 1 of 1
					
				How to export production stats to csv/json from saves?
				Posted: Sun Feb 23, 2025 5:09 pm
				by pilp
				For one of my uni subjects i was tasked to find some data in a time series and do something with them in Py (to learn Py). Normally people would get some publicly available data, but i thought of using production statistics from one of my vanilla Factorio save.
Unfortunately i dont know how to export the necessary data from my save.
Does anyone know, how to export production statistics from my save to csv or json?
Im including a copy of my save in case someone needed it.
Its a vanilla playthrough, doesnt have Space age DLC, game version 2.0.15
			 
			
					
				Re: How to export production stats to csv/json from saves?
				Posted: Sun Feb 23, 2025 6:35 pm
				by eugenekay
				Sounds like the 
Stats Saver mod. This covers "Live" data as you play the game.
If you wanted to get the Historical data for an existing Save you would need to iterate through the 
LuaFlowStatistics using get_flow_count for your chosen items/timeframes; then export using 
write_file.
Good Luck!
 
			
					
				Re: How to export production stats to csv/json from saves?
				Posted: Sun Feb 23, 2025 7:42 pm
				by Muche
				I made the following command (to be run in the 
console):
Code: Select all
local timescales = {
  [defines.flow_precision_index.five_seconds] = "5s",
  [defines.flow_precision_index.one_minute] = "1min",
  [defines.flow_precision_index.ten_minutes] = "10min",
  [defines.flow_precision_index.one_hour] = "1h",
  [defines.flow_precision_index.ten_hours] = "10h",
  [defines.flow_precision_index.fifty_hours] = "50h",
  [defines.flow_precision_index.two_hundred_fifty_hours] = "250h",
  [defines.flow_precision_index.one_thousand_hours] = "1000h"
}
for surface_name, _ in pairs(game.surfaces) do
  local flowdata = game.forces.player.get_item_production_statistics(surface_name)
  for timescale_index, timescale_name in pairs(timescales) do
    local tbl = {}
    local totals = flowdata.input_counts
    for item_name, _ in pairs(totals) do
      local row = {item_name}
      for i = 1, 300 do
        table.insert(row, flowdata.get_flow_count{name=item_name, category="input", precision_index=timescale_index, sample_index=i, count=true})
      end
      table.insert(tbl, row)
    end
    helpers.write_file("stats-" .. surface_name .. "-" .. timescale_name .. ".json", helpers.table_to_json(tbl), false)
  end
end
game.player.print("Export done")
It should export all item production stats into json files in the 
script-output folder.
Item consumption stats could also be available (
output category of 
LuaFlowStatistics), fluids/builds/kills production/consumption (see other 
get_*_statistics of 
LuaForce, electric network, or pollution stats.
 
			
					
				Re: How to export production stats to csv/json from saves?
				Posted: Sun Feb 23, 2025 8:02 pm
				by pilp
				Thank you both for you answers.
Muche wrote: Sun Feb 23, 2025 7:42 pm
I made the following command (to be run in the 
console):
 
Thank you for your code and the file!  
 
 
eugenekay wrote: Sun Feb 23, 2025 6:35 pm
If you wanted to get the Historical data for an existing Save you would need to iterate through the 
LuaFlowStatistics using get_flow_count for your chosen items/timeframes; then export using 
write_file.
 
Thank you for your tip. I will check out the LuaFlowStatistics, how it works, so i can write about it in a report accompanying my Py code.  

 
			
					
				Re: How to export production stats to csv/json from saves?
				Posted: Sun Feb 23, 2025 8:06 pm
				by eugenekay
				Muche wrote: Sun Feb 23, 2025 7:42 pm
I made the following command (to be run in the 
console):
 
Wow, that looks like a good candidate to put on the Wiki for future reference!  I was just spitballing how it could be achieved, and here is a complete implementation. 

 
			
					
				Re: How to export production stats to csv/json from saves?
				Posted: Mon Apr 28, 2025 11:01 am
				by pandafish
				eugenekay wrote: Sun Feb 23, 2025 8:06 pm
Muche wrote: Sun Feb 23, 2025 7:42 pm
I made the following command (to be run in the 
console):
 
Wow, that looks like a good candidate to put on the Wiki for future reference!  I was just spitballing how it could be achieved, and here is a complete implementation. 
 
 
Added it (including Muche as a source) to to 
https://wiki.factorio.com/Production_statistics - This worked wonders. Thanks!