![](https://secure.gravatar.com/avatar/09764ad794a843b7c030ae5ace320f18.jpg?s=120&d=mm&r=g)
Hi, I need a little help here. I need to write some Numeric arrays (coming from some simulations) to ASCII files. I'm sure it's a well known topic and I'd be happy not to have to reinvent the wheel. I've both 1-dim and 2-dim arrays and I'd like to get something like this: - 1-dim array ASCII file: 0.1 0.2 0.3 etc... - 2-dim array ASCII file: 0.1 0.2 0.3 0.4 0.5 0.6 etc.... How can I get them? Thanks in advance, Andrea. --- Andrea Riciputi "Science is like sex: sometimes something useful comes out, but that is not the reason we are doing it" -- (Richard Feynman)
![](https://secure.gravatar.com/avatar/73f4e1ffd23622a339c1c9303615d7fe.jpg?s=120&d=mm&r=g)
"Andrea" == Andrea Riciputi <ariciputi@pito.com> writes:
Andrea> Hi, I need a little help here. I need to write some Andrea> Numeric arrays (coming from some simulations) to ASCII Andrea> files. I'm sure it's a well known topic and I'd be happy Andrea> not to have to reinvent the wheel. I've both 1-dim and Andrea> 2-dim arrays and I'd like to get something like this: Andrea> - 1-dim array ASCII file: Andrea> 0.1 0.2 0.3 etc... If your array is not monstrously large, and you can do it all in memory, do fh = file('somefile.dat', 'w') s = ' '.join([str(val) for val in a]) fh.write(s) where a is your 1D array Andrea> - 2-dim array ASCII file: Andrea> 0.1 0.2 0.3 0.4 0.5 0.6 etc.... Andrea> How can I get them? Same idea fh = file('somefile.dat', 'w') for row in a: s = ' '.join([str(val) for val in row]) fh.write('%s\n' % s) where a is your 2D array The scipy module also has support for reading and writing ASCII files. Note if you are concerned about efficiency and are willing to use binary files, use the fromstring and tostring methods. JDH
![](https://secure.gravatar.com/avatar/09764ad794a843b7c030ae5ace320f18.jpg?s=120&d=mm&r=g)
On 27 Jan 2004, at 19:31, John Hunter wrote:
If your array is not monstrously large, and you can do it all in memory, do
1-dim arrays with 1000 elements and 2-dim arrays with (1000 x 1000) elements have to be considered "monstrously large"? Cheers, Andrea. --- Andrea Riciputi "Science is like sex: sometimes something useful comes out, but that is not the reason we are doing it" -- (Richard Feynman)
![](https://secure.gravatar.com/avatar/73f4e1ffd23622a339c1c9303615d7fe.jpg?s=120&d=mm&r=g)
"Andrea" == Andrea Riciputi <ariciputi@pito.com> writes:
Andrea> On 27 Jan 2004, at 19:31, John Hunter wrote: >> If your array is not monstrously large, and you can do it all >> in memory, do Andrea> 1-dim arrays with 1000 elements and 2-dim arrays with Andrea> (1000 x 1000) elements have to be considered "monstrously Andrea> large"? You should have no trouble with either the 1D or 2D approaches I posted with arrays this size. Even though 1000x1000 is a lot of elements, the 2D approach does the string operations row by row, so only 1000 will be converted at a time, which will be trivial for all but the clunkiest machines. JDH
![](https://secure.gravatar.com/avatar/73f4e1ffd23622a339c1c9303615d7fe.jpg?s=120&d=mm&r=g)
"Andrea" == Andrea Riciputi <ariciputi@pito.com> writes:
Andrea> Hi, I need a little help here. I need to write some Andrea> Numeric arrays (coming from some simulations) to ASCII Andrea> files. I'm sure it's a well known topic and I'd be happy Andrea> not to have to reinvent the wheel. I've both 1-dim and Andrea> 2-dim arrays and I'd like to get something like this: Andrea> - 1-dim array ASCII file: Andrea> 0.1 0.2 0.3 etc... If your array is not monstrously large, and you can do it all in memory, do fh = file('somefile.dat', 'w') s = ' '.join([str(val) for val in a]) fh.write(s) where a is your 1D array Andrea> - 2-dim array ASCII file: Andrea> 0.1 0.2 0.3 0.4 0.5 0.6 etc.... Andrea> How can I get them? Same idea fh = file('somefile.dat', 'w') for row in a: s = ' '.join([str(val) for val in row]) fh.write('%s\n' % s) where a is your 2D array The scipy module also has support for reading and writing ASCII files. Note if you are concerned about efficiency and are willing to use binary files, use the fromstring and tostring methods. JDH
![](https://secure.gravatar.com/avatar/09764ad794a843b7c030ae5ace320f18.jpg?s=120&d=mm&r=g)
On 27 Jan 2004, at 19:31, John Hunter wrote:
If your array is not monstrously large, and you can do it all in memory, do
1-dim arrays with 1000 elements and 2-dim arrays with (1000 x 1000) elements have to be considered "monstrously large"? Cheers, Andrea. --- Andrea Riciputi "Science is like sex: sometimes something useful comes out, but that is not the reason we are doing it" -- (Richard Feynman)
![](https://secure.gravatar.com/avatar/73f4e1ffd23622a339c1c9303615d7fe.jpg?s=120&d=mm&r=g)
"Andrea" == Andrea Riciputi <ariciputi@pito.com> writes:
Andrea> On 27 Jan 2004, at 19:31, John Hunter wrote: >> If your array is not monstrously large, and you can do it all >> in memory, do Andrea> 1-dim arrays with 1000 elements and 2-dim arrays with Andrea> (1000 x 1000) elements have to be considered "monstrously Andrea> large"? You should have no trouble with either the 1D or 2D approaches I posted with arrays this size. Even though 1000x1000 is a lot of elements, the 2D approach does the string operations row by row, so only 1000 will be converted at a time, which will be trivial for all but the clunkiest machines. JDH
participants (2)
-
Andrea Riciputi
-
John Hunter