Fill array for csv export

7 views
Skip to first unread message

Mahdi Ben Jelloul

unread,
Jun 12, 2020, 12:06:59 PM6/12/20
to liam2-users
Dear liam2 users,

I hope you are all doing well.

I try to save a multi-dimensional table in a csv file and the implementation is very time-consuming.

statistiques_etats():
- groupe_age: trunc(age / 10)

- i: 0
- while (i < 14):
- csv(
effectifs_par_sexe_et_valeur('etat', etat, False, i),
fname = 'output/etats.csv',
mode = 'a'
)

- csv(
effectifs_par_sexe_et_valeur('etat', etat, True, i),
fname = 'output/etats.csv',
mode = 'a'
)

- j: 0
- while (j < 14):
- k: 1
- while (k < 8):
- csv(
i, j, period, True, k * 10, (count(sexe and (groupe_age == k) and (etat == i) and (lag(etat) == j)) * POIDS),
fname = 'output/matrice_transition.csv',
mode = 'a'
)

- csv(
i, j, period, False, k * 10, (count( (not sexe) and (groupe_age == k) and (etat == i) and (lag(etat) == j)) * POIDS),
fname = 'output/matrice_transition.csv',
mode = 'a'
)
- k: k + 1

- j: j + 1

- i: i + 1

We use a loop that opens a csv file (and closes it) at every step.  That is very time consuming. 
Is it possible to fill in a multidimensional array and write it in a csv file at once ?
Otherwise, is it possible to keep the csv file open ?

If you encountered a similar problem, how did you deal with it ? 

Thank you and stay safe !

Mahdi Ben Jelloul

Gaëtan de Menten

unread,
Jun 17, 2020, 3:54:47 AM6/17/20
to liam2...@googlegroups.com

Hello Mahdi,

 

Sorry, I don't understand what you are trying to achieve. This seems awfully complicated and inefficient indeed because saving a multi-dimensional table to a .csv file is normally as simple as:

 

csv(my_table_or_array, fname='my_filename.csv')

 

I guess there is something I am missing. Could you provide a self-contained (runnable) example reproducing the kind of problem you are having?

 

Keeping the csv file open is not possible at the moment.

 

Gaëtan

 

--
You received this message because you are subscribed to the Google Groups "liam2-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to liam2-users...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/liam2-users/CA%2BAkwSd1wZ303yW7%3Dji%3DfzMp8jFwRKkSagcqaR6bkquW_%2BS-yg%40mail.gmail.com.



Disclaimer: This e-mail may contain confidential information which is intended only for the use of the recipient(s) named above.
If you have received this communication in error, please notify the sender immediately and delete this e-mail from your system.
Please note that e-mail messages cannot be considered as official information from the Federal Planning Bureau.

Mahdi Ben Jelloul

unread,
Jun 17, 2020, 4:00:36 AM6/17/20
to liam2-users
Hi Gaëtan,

Sorry if my question was not clear.
I do know how to save an array to a  file
I would like to know how to build such an array/table within a loop using liam2 syntax.

Thanks

Mahdi Ben Jelloul


Gaëtan de Menten

unread,
Jun 17, 2020, 4:29:31 AM6/17/20
to liam2...@googlegroups.com

Ah, ok… Creating such arrays "cell by cell" is not possible at the moment.

 

Gaëtan

 

Gaëtan de Menten

unread,
Jun 19, 2020, 3:05:13 AM6/19/20
to liam2...@googlegroups.com

For what it's worth, I just implemented something akin to "setitem" on any entity variable to set subsets of variables instead of whole columns. This lives in the devel branch (I hope to merge it to master "soonish").

 

You will now be able to write:

 

    my_array[subset]: value

 

Note that it does not work (yet) for global arrays (and I am unsure how hard it will be to implement this, so I don't know if it will make it to 0.13) and the array needs to exist beforehand.

 

Gaëtan

Reply all
Reply to author
Forward
0 new messages