Example of how to write line by line to a CSV file in Julia?

1,266 views
Skip to first unread message

yaois...@gmail.com

unread,
Sep 16, 2014, 4:52:53 PM9/16/14
to julia...@googlegroups.com
Hi Guys,

I'm struggling to find some examples regarding how to write line by line to a CSV file. Scouring the documentation, I have been able to come across this so far: http://julia-cn.readthedocs.org/en/latest/stdlib/base/?highlight=writecsv
So I currently have a function that is part of a for loop that outputs some data in each iteration.
In pseudocode, here is what I want to do:

for i in 1:2000
    y1,y2,y3 = f(i)
    ysavetuple = y1,y2,y3
    writecsv("ysavetuple.csv",ysavetuple)

However, I am not sure how to go about saving line by line, separating the tuple into individual cells under labeled column headers.

Any guidance would be appreciated.

Thanks,
Wally

Leah Hanson

unread,
Sep 16, 2014, 5:06:25 PM9/16/14
to julia...@googlegroups.com
Is this what you're looking for?

~~~
julia> csvfile = open("ysavetuple.csv","w")
IOStream(<file ysavetuple.csv>)

julia> write(csvfile,"ColName A, ColName B, ColName C\n")
32

julia> foo(i) = tuple(rand(Int,3)...)
foo (generic function with 1 method)

julia> for i = 1:20
         y1,y2,y3 = foo(i)
         ysavetuple = y1, y2, y3
         write(csvfile, join(ysavetuple,","), "\n")
       end

julia> close(csvfile)

shell> cat ysavetuple.csv
ColName A, ColName B, ColName C
3341065172693628568,2785375663699326345,3139303717694603190
6079317564046002747,-9078602496166771942,316842938125726587
-3417130537836373451,-2125006311387038993,7377203727928183519
8358027301276313215,-550007455247301253,4550810412861259284
6923216407498203989,-6554142179327706075,-7691369716492508716
-1686152429396775777,8749188497040439214,-7530985225065041867
-1703823086470161742,8611509687251940012,4568676596067050636
5799892753953213008,3071911357874547504,-5404969669875969538
-2454300728611454039,6075726156412295188,-2730595046142414415
-4745225085034107111,6326943245021453770,-1335744683672406184
-8241065834050575249,9081437527204936820,-5382908887176747766
3809111146841985672,8705023102801762616,-7272701245961853612
8636161548674726627,5531655905953625247,-2539551872166338494
-1075777440989646159,1220494169889323606,3141159378055978579
2137393503615038568,-1785476893677818827,-411925966250929747
-5315012022696729804,858923725586425906,-7987423971277696135
3115970830776320266,6147819354500740390,-9153883089584687671
-5801765622147987138,-6846434057707600414,-850327360071980009
-4113725585379526907,5602712312389731368,2811295763696425064
-1191524940193068432,-3389508986455397789,2511100231858228670
~~~

-- Leah

yaois...@gmail.com

unread,
Sep 16, 2014, 5:11:39 PM9/16/14
to julia...@googlegroups.com
Leah, thank you for saving the day again.

Regards,
Wally

Jacob Quinn

unread,
Sep 17, 2014, 9:03:25 AM9/17/14
to julia...@googlegroups.com
Sounds like you should change your email to leahis...@gmail.com! :)
Reply all
Reply to author
Forward
0 new messages