常用处理数据方法
一.Excel文件
[matlab]
1.讀取表格
函數(shù)
[numericData, textData, rawData, customOutput] = xlsread(file,
sheet, range, mode, customFun)*
numericData是excel里的數(shù)據(jù)
textData是文本內(nèi)容
rawData是未處理過的數(shù)據(jù)
file是文件路徑
sheet是選擇第幾個(gè)表,可填入字符,也可寫入數(shù)字,
range選擇指定數(shù)據(jù),如’D2:D3’
常用用法
[num,txt,raw]=xlsread(‘C:\Users\Administrator\Desktop\test\a.xls’)
%num返回的是excel中的數(shù)據(jù),txt輸出的是文本內(nèi)容,row輸出的是未處理數(shù)據(jù)
%一般情況下,我們讀取的是excel中的數(shù)劇,所以可以直接用下面的,只輸出數(shù)據(jù)矩陣便可
[num]=xlsread(‘C:\Users\Administrator\Desktop\test\a.xls’)
[num]=xlsread(‘C:\Users\Administrator\Desktop\test\a.xls’,2)
讀取excel中的第二個(gè)sheet中的數(shù)據(jù)
2.寫入表格
函數(shù)
[success,theMessage]=xlswrite(file,data,sheet,range)
file寫入目標(biāo)文件路徑
data寫入數(shù)據(jù)
sheet寫入目標(biāo)文件第幾個(gè)表格
range寫入目標(biāo)文件表格范圍
用法
xlswrite(‘data1.xlsx’,ARecd,3,‘G2:G1733’)
將ARecd中的數(shù)據(jù)寫入data1下sheet3中的G2:G1733.
[python]
可參考這篇博文:Python3讀寫Excel文件
讀取和寫入數(shù)據(jù)
兩個(gè)庫(kù)xlrd和xlwt
xlrd用來(lái)讀表格
xlwt用來(lái)寫表格
openpyxl和xlsxwriter都用來(lái)寫數(shù)據(jù)
寫表格代碼
初始化
workbook = xlwt.Workbook(encoding = 'utf-8') worksheet = workbook.add_sheet('My Worksheet')寫數(shù)據(jù)
worksheet.write(1,1,'1')保存
workbook.save('shuju.xls')其他用法
讀表格代碼
打開文件
xlrd.open_workbook(r'/root/excel/chat.xls')獲取要打開的sheet文件
# 獲取所有sheetsheet_name = workbook.sheet_names()[0]# 根據(jù)sheet索引或者名稱獲取sheet內(nèi)容sheet = workbook.sheet_by_index(0) # sheet索引從0開始獲取指定單元格里面的值
sheet.cell_value(第幾行,第幾列)獲取某行或者某列的值
# 獲取整行和整列的值(數(shù)組)rows = sheet.row_values(1) # 獲取第2行內(nèi)容cols = sheet.col_values(2) # 獲取第3列內(nèi)容不建議使用xlwt模塊寫入數(shù)據(jù),在xlwt中生成的xls文件最多能支持65536行數(shù)據(jù)。
xlsxwriter這個(gè)模塊,它生成的文件后綴名為.xlsx,最大能夠支持1048576行數(shù)據(jù),16384列數(shù)據(jù)
os常用操作:https://www.cnblogs.com/delav/p/7693744.html
總結(jié)
- 上一篇: 数学建模记录
- 下一篇: DOSbox汇编环境配置