以下檔案輸出、讀取的範例都以一個2D numpy array作為例子。
先用np.arange與reshape生成一個元素為0~99的10x10二維陣列(請參考Numpy Array單元)
We will generate a 10x10 numpy array using the np.arange and reshape functions first:
A = np.arange(100).reshape(10,10) #convert the 100x1 array to a 10x10 array
print(A)
[[ 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 36 37 38 39]
[40 41 42 43 44 45 46 47 48 49]
[50 51 52 53 54 55 56 57 58 59]
[60 61 62 63 64 65 66 67 68 69]
[70 71 72 73 74 75 76 77 78 79]
[80 81 82 83 84 85 86 87 88 89]
[90 91 92 93 94 95 96 97 98 99]]
在使用numpy提供的指令讀寫檔案之前,
「不」需要先開啟檔案,直接在指令中提供檔名就可以了
Let's output this numpy array to a text file using the numpy command.
Note that you do not need to "open" the file first, if using the numpy command for input or output. Simply provide the file name when calling the numpy command.
np.savetxt 輸出到文字檔 output to a text file¶
1. 語法 :
numpy.savetxt(fname, var, header='...', footer='...', comments='...')
2. 變數 :
fname : 檔案名稱
var : 要被儲存之變數名稱
header : 檔頭文字(在NumPy 1.7.0.版後開始支援)
footer : 檔尾文字(在NumPy 1.7.0.版後開始支援)
comments : 註解文字 預設: '#'(在NumPy 1.7.0.版後開始支援) -->在檔頭、檔尾那一列的開頭會加上此處指定的符號,之後用numpy指令讀取時,會被判斷為註解(或檔頭)
參考連結 : https://docs.scipy.org/doc/numpy/reference/generated/numpy.savetxt.html
np.loadtxt 讀取文字檔 read from a text file¶
1. 語法 :
numpy.loadtxt(fname, comments='...', skiprows=..., usecols=..., unpack=...)
2. 變數 :
fname : 檔案名稱
comments : 註解符號,預設為'#'。
skiprows : 跳過前面n行(預設為0)
usecols : 要讀入的直行數(預設為所有的直行都讀入)
unpack : 是否要將一個直行讀取成一個1D array,預設為關閉(False),If set to 'True", x, y, z = loadtxt(...)
3. 回傳 :
ndarray
參考連結 : https://docs.scipy.org/doc/numpy/reference/generated/numpy.loadtxt.html
# example 1: read the entire text file and assign as a new array
Ain = np.loadtxt('Aout.txt')
print(Ain)
print(Ain.shape)
[[ 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. 36. 37. 38. 39.]
[ 40. 41. 42. 43. 44. 45. 46. 47. 48. 49.]
[ 50. 51. 52. 53. 54. 55. 56. 57. 58. 59.]
[ 60. 61. 62. 63. 64. 65. 66. 67. 68. 69.]
[ 70. 71. 72. 73. 74. 75. 76. 77. 78. 79.]
[ 80. 81. 82. 83. 84. 85. 86. 87. 88. 89.]
[ 90. 91. 92. 93. 94. 95. 96. 97. 98. 99.]]
(10, 10)
# example 2: skip the first three rows, and also skip the 2nd, 4th, 6th...columns
Ain = np.loadtxt('Aout.txt',skiprows=3, usecols=(0,2,4,6,8))
print(Ain)
print(Ain.shape)
# example 3: read a file with header (with # a the beginning);
# only read the 1st, 3rd, and 5th columns, and assign each column as a 1-D array separately
x,y,z = np.loadtxt('Aout_h.txt',usecols=(0,2,4),unpack=True)
print(x)
print(x.shape)
print(z)
np.fromfile 讀取二進位檔 read from a binary file¶
1. 語法 :
numpy.fromfile(file, dtype=..., count=n)
2. 變數 :
file : 讀取檔案之路徑
dtype : float or int (default=float)
count : only read the first n values from file (if n=-1, read entire file) (default n=-1)
3. 回傳 :
1D array
參考連結 :https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.fromfile.html#numpy.fromfile
# Example for binary I/O:
A=np.arange(10) # generage an array with integers from 0 to 10
print(A)
# Output the array to a binary file using the Python built-in write command
# Note: Remember to "open" the file first!! close after output.
fo2 = open("binary.dat", "wb")
fo2.write(A)
fo2.close()
# Read the binary file using the Numpy.fromfile command
# Because the data were output as integer, when reading it, we need to set the dtype=int
B=10*np.fromfile("binary.dat",dtype=int)
print(B)
# Lastly, combine three 1-D arrays as columns using Numpy.column_stack;
# output to a text file with header line using Numpy.savetxt
np.savetxt('binary.txt',np.column_stack((A,B,A+B)),header=' A B A+B')