2

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()
3
  • Also, there are three spaces between each number, but I want it to be 1 space. Commented Sep 10, 2012 at 17:45
  • @JoranBeasley, that's the answer; why don't you submit it? Commented Sep 10, 2012 at 17:49
  • go ahead and accept answer if it resolved your problem :) Commented Sep 10, 2012 at 17:58

2 Answers 2

3

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 :)
Sign up to request clarification or add additional context in comments.

3 Comments

Would you mind editing your solution so that it uses with open(...) as f: instead of the explicit .close()?
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.
there used with instead (better form... but unrelated to OP)
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.