Array into a TXT file without [[ ]]

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • smarras
    New Member
    • Jul 2007
    • 1

    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=o pen('msl_data_m odel.txt','a')
    outfile_model.w rite(str(data_v ar_model[time,:,:])+' ')
    outfile_model.w rite('\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
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Originally posted by smarras
    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=o pen('msl_data_m odel.txt','a')
    outfile_model.w rite(str(data_v ar_model[time,:,:])+' ')
    outfile_model.w rite('\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:[code=Python]dataList = []
    for time in range(data_var_ model.shape[0]):
    dataList.append (data_var_model[time,:,:]) # this line contains an invalid slice
    outfile_model=o pen('msl_data_m odel.txt','a')
    outfile_model.w rite('\n'.join([str(d[0][0]) for d in dataList]))
    outfile_model.c lose()[/code]

    Comment

    Working...