python如何讀取.mtx文件
mtx文件是按照稀疏矩陣格式存儲(chǔ)的矩陣數(shù)據(jù),可以按照以下步驟讀取:
1、安裝scanpy包pip install scanpy2、文件讀取
import scanpy as sc adata = sc.read(filename)data = adata.X
第一行read之后返回的是annData,第二行通過(guò).X操作得到的是矩陣數(shù)據(jù)
3、轉(zhuǎn)換為稠密矩陣data = data.todense()
直接得到的矩陣是稀疏形式的,通過(guò)todense函數(shù)可轉(zhuǎn)換為稠密矩陣
補(bǔ)充:python讀取各種文件方式
Json:use_time=[]with open(address,’r’) as f: #ubuntu mobile = json.load(f) calls = mobile['transactions'][0]['calls']for call in calls: use_time.append(str(call[’use_time’]))Excel:
rawdata1=open_workbook(address)rawdata=rawdata1.sheet_by_index(0)for i in range(1,rawdata.nrows): if rawdata.cell(i,date_index).value=='': #跳過(guò)空行continue else:if ctype==3: #若為3,則用datetime模塊處理日期 date1=rawdata.cell(i,date_index).value date2 = xldate_as_tuple(date1,0) date3=datetime(*date2) if '.' in str(rawdata.cell(i,phone_index).value):phone1=str(rawdata.cell(i,phone_index).value)[:-2] else:phone1=str(rawdata.cell(i,phone_index).value)寫(xiě)EXCEL:
Excel_file = xlwt.Workbook() sheet = Excel_file.add_sheet(’sheet0’)header=[u’號(hào)碼’,’日期top1’,’日期top2’,’日期top3’]#寫(xiě)入標(biāo)題行:for i in range(len(header)): sheet.write(0,i,header[i])#開(kāi)始按行寫(xiě)入數(shù)據(jù):for i in range(len(phonelist)): sheet.write(i+1,0,phonelist[i]) sheet.write(i+1,1,dic[str(phonelist[i])])#保存EXCEL:Excel_file.save('C:/Users/Desktop/100個(gè)文件輸出xls/'+str(fileName)+'.xls')CSV:
rawdata=pd.read_csv(address,skip_blank_lines=True) #參數(shù)為去除空行if ’start_time’ or ’begin_time’ in rawdata.columns: if ’start_time’ in rawdata.columns:start_time=rawdata[’start_time’] elif ’begin_time’ in rawdata.columns: start_time=rawdata[’begin_time’]txt:
rawdata=open(address,’r’)i=0a=[] #c存放第一行的列名for line in rawdata: if i==1: #默認(rèn)第二行開(kāi)始存儲(chǔ)通話(huà)數(shù)據(jù)a=line.split(’,’) #逗號(hào)作為分隔符for j in range(len(a)): #查找指定列名所在的列下標(biāo) if ((’-’ in str(a[j]))or(’/’ in str(a[j]))): #判斷日期所在列數(shù)date_index=j #保存日期的列下標(biāo) elif str(a[j]).isdigit() and len(str(a[j]))>5: #默認(rèn)全為數(shù)字組成的字符串為電話(huà)號(hào)碼phone_index=j else:passbreak else:i+=1i=0for line in rawdata:#開(kāi)始轉(zhuǎn)存數(shù)據(jù): if len(line)<10: #跳過(guò)空行continue data_line=line.split(’,’) #txt默認(rèn)以’,’分隔數(shù)據(jù) if i==0:pass #第一行為列名,跳過(guò)i+=1 else: #從第二行開(kāi)始保存數(shù)據(jù)start_time.append(data_line[date_index])
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章:
1. JSP+Servlet實(shí)現(xiàn)文件上傳到服務(wù)器功能2. 利用ajax+php實(shí)現(xiàn)商品價(jià)格計(jì)算3. 利用FastReport傳遞圖片參數(shù)在報(bào)表上展示簽名信息的實(shí)現(xiàn)方法4. chat.asp聊天程序的編寫(xiě)方法5. 網(wǎng)頁(yè)中img圖片使用css實(shí)現(xiàn)等比例自動(dòng)縮放不變形(代碼已測(cè)試)6. PHP循環(huán)與分支知識(shí)點(diǎn)梳理7. jsp實(shí)現(xiàn)textarea中的文字保存換行空格存到數(shù)據(jù)庫(kù)的方法8. JSP之表單提交get和post的區(qū)別詳解及實(shí)例9. Ajax請(qǐng)求超時(shí)與網(wǎng)絡(luò)異常處理圖文詳解10. jsp實(shí)現(xiàn)登錄驗(yàn)證的過(guò)濾器
