What's the most straightforward way to read and write numerical data from a file in Julia?
I'm interested in picking up Julia as a scientific computing language, hopefully to replace my current system of Fortran-for-speed and Python-for-convenience. However, I'm getting hung up on its file handling.
The issue is that in Julia, all of my options for file I/O look pretty confusing. The built-in read and write functionality seems to only take text input as strings, rather than as numeric types. The CSV package is powerful, but then it complicates things because it only returns a 'dataframe', whatever that is. Likewise, the documentation for CSV.write() says that it will "write a Tables.jl interface input to a csv file." All I want to do is read some numbers into a couple 1D arrays, do some math on them, then write them back out. I feel like I must be missing something stupidly obvious, because I don't understand why it seems so hard for Julia to do something that creaky old Fortran figured out decades ago.
To illustrate more clearly what I'm trying to do, sample code follows in Fortran 90 and Python. In both cases, the code reads an input file (file.txt) and stores the data in 1D arrays a and b. Then, assuming further computations follow producing 1D arrays x and y, those two arrays are written to a new file (file2.txt).
Fortran 90:
open(10, 'file.txt')
do i = 1, 10
read(10, *) a(i), b(i)
end do
close(10)
...
(further computations on a and b, producing outputs x and y)
...
open(11, 'file2.txt')
do i = 1, 10
write(10, *) x(i), Y(i)
end do
close(11)
Python:
a, b = numpy.loadtxt("file.txt", unpack=True)
...
(further computations on a and b, producing outputs x and y)
...
data = numpy.column_stack((x, y))
numpy.savetxt(file2.txt, data, fmt='%.16E', delimiter=' ')
As you can see, the Fortran method is a little clunky but very explicit about what it's doing, whereas the Python method is slick but requires a little extra formatting work.