Lesson 01 Matplotlib::output data 產生兩列數據,並存至檔案
Lesson 1: 產生兩列數據,並存至檔案
Code
- f = open("workfile", "w");
- for x in range(1, 11):
- f.write(str(x) +" "+ str(x*x) + "\n");
- f.close();
Note and Comments
- f = open("workfile", "w"); 開啟一個檔案供程式寫入資料
- for x in range(1, 11): for迴圈, 產生一個用x計數的迴圈, 數值由1到10為止. 在迴圈的內容前須以":"標示宣告結束
- f.write(str(x) +" "+ str(x*x) + "\n"); 以字串(string)的形式寫x與x平方到檔案中, \n是換行符號. 注意f.write....前的空白是"Tab"鍵, 不是空白. python以Tab縮排來標記執行內容
- f.close(); 關閉檔案
how to run the py code with the console 如何執行程式
- 將code 存入一個以.py為附檔名的檔案, 例如pythonOpen.py. 在命令列中打入python pythonOpen.py 執行程式
Lesson 01.2 Matplotlib::output data with scipy產生兩組數據,並存至檔案
Lesson 01.2: 利用scipy, 產生兩組數據,並存至檔案
Code
- f = open("workfile", "w");
- import scipy as sp
- data=[range(10),range(0,20,2)]
- sp .savetxt('workfile.txt', data )
Note and Comments
- import scipy as sp 引入scipy套件庫,並命名爲sp
- data=[range(10),range(0,20,2)] 利用range函數產生兩組數列
- sp.savetxt('workfile.txt', data ) 利用sp套件庫ㄓㄘㄥ 的savetxt函數儲存檔案, 上圖為檔案格式