0

I have a few lists which I all want to export to the same .txt file.

So far I only export 3 of the lists using

my_array=numpy.array(listofrandomizedconditions)
my_array2=numpy.array(inputsuser)
my_array3=numpy.array(reactiontimesuser)
combined=numpy.column_stack([my_array,my_array2,my_array3])
numpy.savetxt(participantnumber + ".txt", combined, delimiter=" ", fmt ="%-12s") 

This gives me an output like

CongruentPositief no input or wrong button no reactiontime
IncongruentNegPos no input or wrong button no reactiontime

Since this is quite hard to read I want to add a tab between all the different lists.

Also I want to add a few lists which aren't 192 elements long unlike the first 3, but then I get an error that every array has to be the same size. Is there a way around this?

ilyas patanam
  • 5,116
  • 2
  • 29
  • 33
Joris
  • 1
  • 4

1 Answers1

0

Especially if you are starting with lists (of strings) I don't see the point to using numpy arrays.

For a start just try printing values:

In [659]: conditions=['one','two','three']
In [660]: values=[1,2,3]
In [661]: other=['xxxx','uuuuuuu','z']

basic format

In [662]: for xyz in zip(conditions, values,other):
    print("%s,%s,%s"%xyz)
   .....:     
one,1,xxxx
two,2,uuuuuuu
three,3,z

refined with tab and fixed lengths:

In [663]: for xyz in zip(conditions, values,other):
    print("%-12s\t%-12s\t%-12s"%xyz)
   .....:     
one             1               xxxx        
two             2               uuuuuuu     
three           3               z     

Next step is to open a file and write to that, instead of print.

It's column stack that requires equal length strings. savetxt just creates a fmt string from your parameter (and the number of columns), and writes each row like I do.


In [667]: with open('temp.txt','w') as f:
   .....:     for xyz in zip(conditions,values,other):
   .....:         f.write('%-12s,%-12s,%-12s\n'%xyz)
   .....:         
In [668]: cat temp.txt
one         ,1           ,xxxx        
two         ,2           ,uuuuuuu     
three       ,3           ,z  
hpaulj
  • 221,503
  • 14
  • 230
  • 353