Question

How do I write to csv file, naming the file using the value of the first row?

  • 25 April 2024
  • 4 replies
  • 47 views

Badge +2

How do I write a csv file and name it using the value in the top / first row?
I want just one file to be created, (not multiple files like fanout creates) named after the entry in the first row.

For example in the table here I have 9 rows and 2 columns. I want to create a csv of all of this data but named “20231207.csv”

 

As a secondary question, you will see the first attribute column is sorted into date order. If this wasn’t sorted into order, what is the quickest way of identifying the closest date to the current date, and then using that identified value to name the csv file?

Hopefully this is something really simple!

Thanks in advance.

Stu


4 replies

Userlevel 4
Badge +13

Starting with your secondary question. With the dates formatted how you have them, a simple solution could be to sort numeric descending. A more robust solution would be to use a DateTimeStamper to create a new attribute for the current date, and then use a DateTimeCalculator to determine the interval between the input dates and the current date to find the most recent.

For your primary question, there are probably a lot of ways you could do this. One way would be to use a Sampler on your now sorted dates to get the feature with the most recent date. An attribute manager can be used to rename the CompleteDate to something like MostRecent, and remove all other attributes. Then you can use a featuremerger with a join on of 1 and 1, to append that attribute to all features (including the one that was sampled). When you go to write out your data you can fanout on that attribute, but not write it to the csv.

Userlevel 4
Badge +13

Here is an example of my methods, using similar demo data to what you provided. FME 2021

 

Userlevel 2
Badge +11

I know that the AttributeManager is the preferred answer, but in this scenario I find it easier to configure using a VariableSetter and VariableRetriever.

 

Badge +2

Thank you to both @DanAtSafe and @liamfez for those excellent solutions. You two are legends! 😀 

Reply