I used numpy to do this. I have a text file containing a series of numbers:

[  11.1   44.0   74.9  103.8  115.8
  157.0   170.1  208.4   239.9  296.8]

How do I convert the text file to:

11.10377777 44.03133786 74.9749492 103.83874619 115.83058441 157.0862515 170.10200524 208.4376871 239.90138829 296.86073327

stim = a[0,61:71] 
stim2 = a[0,21]
fname = 'blah'
f_events = open('L:\\directory\\' + blah + '.txt',"w")
f_events.write(str(stim-stim2))
f_events.close()
share|improve this question
Also, there are three spaces between each number, but I want it to be 1 space. – user1636354 22 hours ago
1  
" ".join(map(str,my_list)) – Joran Beasley 22 hours ago
@JoranBeasley, that's the answer; why don't you submit it? – scott654 22 hours ago
It worked, thanks so much. – user1636354 22 hours ago
go ahead and accept answer if it resolved your problem :) – Joran Beasley 22 hours ago
feedback

2 Answers

up vote 2 down vote accepted

use " ".join ... but you need to map your floats to string inside the join

with open('L:\\directory\\' + blah + '.txt',"w" ) as f_events:
    f_events.write(" ".join(map(str,stim-stim2)))  #this line :)
share|improve this answer
Would you mind editing your solution so that it uses with open(...) as f: instead of the explicit .close()? – Jan-Philip Gehrcke 22 hours ago
I'd add that if you want some more customization(e.g. print only first x decimal places), you can use string formatting to get the desired result. Even though this doesn't seem required by the OP. – Bakuriu 22 hours ago
there used with instead (better form... but unrelated to OP) – Joran Beasley 22 hours ago
feedback

Since you are using numpy, just use np.savetxt, really should first check what the packages you use provide...

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.