Reputation: 849
import numpy as np
f=open("a.txt","wb")
a=np.zeros((10,36))
for i in range(10):
for j in range(36):
a[i][j]=i+j
for b in a:
print >>f, b
At first, I think it will occupy 10 lines in the a.txt file with each row in array a a line. But the truth is, the file looks like this:
It can be seen that each row in a occupies three lines in the file. Why?
Upvotes: 0
Views: 219
Reputation: 21
When you're iterating your matrix in the second loop, variable b
is still a numpy object. When you try to print this numpy object, python first gets its string representation.
Numpy has built-in formatting for pretty-printing this numpy object, which is the reason for the multiple lines.
Upvotes: 2
Reputation: 155
Numpy formats the data when it outputs it. However, SciPy doc shows you how you can pimp the output, using for example array2string:
import numpy as np
a = np.zeros((10, 36))
for i, line in enumerate(a):
for j, column in enumerate(line):
a[i][j] = i + j
with open("a.txt","wb") as f:
f.write(np.array2string(a, max_line_width=1000))
Here, with a max_line_width of 1000, each line of the file can be 1000 chars wide.
The file:
[[ 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35.]
[ 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36.]
[ 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37.]
[ 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38.]
[ 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39.]
[ 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40.]
[ 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41.]
[ 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42.]
[ 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43.]
[ 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44.]]
Upvotes: 1
Reputation: 160417
You can see by the documentation that the default value for max_line_width
is 75. After that numpy issues a \n
character. Since you're printing an ndarray
with b
this is what is used.
To change this you can either set the set_printoptions
globally, as already suggested, or if you only want this effect limited to writing in the file you can change your loop to printing by using np.array_str
:
for b in a:
# tweak the line width to what suits you:
print >>f, np.array_str(c, max_line_width=300)
Upvotes: 0
Reputation: 76
Maybe it woule be better use the numpy function savetxt
. Something like this:
import numpy as np
a = np.zeros((10,36))
for i in range(10):
for j in range(36):
a[i][j] = i + j
np.savetxt("a.txt",a,'%2d',newline="\r\n")
So it does the newline only after row of the array :)
Edit:
http://docs.scipy.org/doc/numpy/reference/generated/numpy.savetxt.html
Upvotes: 0
Reputation: 761
The thing is that when you're iterating your matrix in the second loop, b
is still numpy object. When you try to print some object, python first gets its string representation. Numpy has built-in formatting for pretty-printing objects, which is actually called. You can fix it by converting to list in the last line: print >>f, list(b)
Upvotes: 0
Reputation: 1467
Numpy limits line length to 75 by default. You can change the default by np.set_printoptions(linewidth=1000)
Upvotes: 2