471,330 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

Array into a TXT file without [[ ]]

Good morning, I am new to python, but I need it for my research in computational climate simulations for its power.

I need to write a 2D array into a text file, but what I obtain is the correct array, although each value appears within [[ ]];
I use the scripting written below, how should I modify it in order to avoid it to write the parenthesis?

for time in range(data_var_model.shape[0]):
outfile_model=open('msl_data_model.txt','a')
outfile_model.write(str(data_var_model[time,:,:])+' ')
outfile_model.write('\n')

With this routine the output looks this way:

[[ [ 101135.46875 ]]
[ [ 101137.046875 ]]
[ [ 101132.5546875]]
[ [ 101133.2109375]]
[ [ 101131.5703125]]
[ [ 101131.65625 ]]
[ [ 101135.890625 ]]
[ [ 101133.203125 ]]
[ [ 101130.5234375]]
[ [ 101134.0390625]]]


Thanks a lot,
simone
Jul 20 '07 #1
1 7169
bvdet
2,851 Expert Mod 2GB
Good morning, I am new to python, but I need it for my research in computational climate simulations for its power.

I need to write a 2D array into a text file, but what I obtain is the correct array, although each value appears within [[ ]];
I use the scripting written below, how should I modify it in order to avoid it to write the parenthesis?

for time in range(data_var_model.shape[0]):
outfile_model=open('msl_data_model.txt','a')
outfile_model.write(str(data_var_model[time,:,:])+' ')
outfile_model.write('\n')

With this routine the output looks this way:

[[ [ 101135.46875 ]]
[ [ 101137.046875 ]]
[ [ 101132.5546875]]
[ [ 101133.2109375]]
[ [ 101131.5703125]]
[ [ 101131.65625 ]]
[ [ 101135.890625 ]]
[ [ 101133.203125 ]]
[ [ 101130.5234375]]
[ [ 101134.0390625]]]


Thanks a lot,
simone
It may help if you could show us what this object is: data_var_model
I'll take a shot anyway:
Expand|Select|Wrap|Line Numbers
  1. dataList = []
  2. for time in range(data_var_model.shape[0]):
  3.     dataList.append(data_var_model[time,:,:]) # this line contains an invalid slice
  4. outfile_model=open('msl_data_model.txt','a')
  5. outfile_model.write('\n'.join([str(d[0][0]) for d in dataList]))
  6. outfile_model.close()
Jul 20 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

11 posts views Thread by Jeevan | last post: by
8 posts views Thread by Michel Rouzic | last post: by
2 posts views Thread by melanieab | last post: by
4 posts views Thread by Haydnw | last post: by
5 posts views Thread by =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.