Python读取.mtx稀疏矩阵文件及常见数据格式处理指南

白榆
白榆
发布时间:2026-10-14 | 3人浏览过
原创

本文详细介绍如何使用Python中的scanpy库读取.mtx格式的稀疏矩阵文件。通过sc.read()获取AnnData对象,利用.X属性提取矩阵数据,并调用todense()方法将其转换为稠密矩阵。此外,文章还补充了JSON、Excel、CSV及TXT等常见数据文件的读取与写入示例,为数据处理提供全面参考。

.mtx文件采用稀疏矩阵格式存储矩阵数据,可通过以下步骤进行读取操作:

1、安装scanpy包


pip install scanpy

2、文件读取


import scanpy as sc 
adata = sc.read(filename)
data = adata.X

执行第一行代码后返回的是AnnData对象,第二行通过.X属性操作即可得到具体的矩阵数据。

3、转换为稠密矩阵


data = data.todense()

直接读取得到的矩阵为稀疏形式,利用todense函数可将其转换为稠密矩阵。

补充: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=="": #跳过空行
        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)

写EXCEL:


Excel_file = xlwt.Workbook() 
sheet = Excel_file.add_sheet('sheet0')
header=[u'号码','日期top1','日期top2','日期top3']
#写入标题行:
for i in range(len(header)):
    sheet.write(0,i,header[i])
#开始按行写入数据:
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个文件输出xls/"+str(fileName)+".xls")

CSV:


rawdata=pd.read_csv(address,skip_blank_lines=True) #参数为去除空行
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=0
a=[] #c存放第一行的列名
for line in rawdata:
    if i==1: #默认第二行开始存储通话数据
        a=line.split(',') #逗号作为分隔符
        for j in range(len(a)): #查找指定列名所在的列下标
            if (('-' in str(a[j]))or('/' in str(a[j]))): #判断日期所在列数
                date_index=j #保存日期的列下标
            elif  str(a[j]).isdigit() and len(str(a[j]))>5: #默认全为数字组成的字符串为电话号码
                phone_index=j
            else:
                pass
        break
    else:
        i+=1
i=0
for line in rawdata:#开始转存数据:
    if len(line)<10: #跳过空行
        continue
    data_line=line.split(',') #txt默认以','分隔数据
    if i==0:
        pass #第一行为列名,跳过
        i+=1
    else: #从第二行开始保存数据
        start_time.append(data_line[date_index])

以上内容为个人经验总结,旨在提供参考。如有错误或未考虑周全之处,欢迎指正。

相关标签:

本站声明

本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。

Python 相关产品和应用

全部下载

专题列表