python读取excel合并单元_python 读写excel (合并单元格)

本文介绍了如何使用Python的xlrd和xlwt库进行Excel文件的读写操作,特别是处理合并单元格的功能。通过示例代码展示了如何读取Excel,合并单元格并保存到新的Excel文件中,涉及化疗位点信息处理和数据合并。
摘要由CSDN通过智能技术生成

第一次使用python来处理excel,主要是为了记录下合并单元格,单元格样式

示例:从左边 变成右边,将第一列的 实现合并单元格

图片.png

工具:python3, xlrd ,xlwt

import xlrd

import sys

import xlwt

import os

#根据化疗位点,来提取突变检出

def Chemo25Sites(path):

chemosites = dict()

file = open(path)

lines = file.readlines()

rows = len(lines)

for i in range(1, rows):

aa = lines[i].strip().split('\t')

chr, pos, rsid, ref, wild_type = aa

chemosites[rsid] = wild_type+'\t'+chr+'\t'+pos+'\t'+ref

file.close()

return chemosites

def getChemoVariants(path, vcf):

#vcf : sample.vatriants_all.vcf

chemosites = Chemo25Sites(path)

outfile = open('./chemo_result_genotype.txt','w')

chemovariants = dict()

for key in chemosites.keys():

cmd = 'grep -w {key} {infile}'.format(key=key, infile=vcf)

res = os.popen(cmd).readlines()

if len(res) == 0: #该化疗位点没有检出

value = chemosites[key].split('\t')

genotype, chrom, pos, ref = value

#print('\t'.join([key, chrom, pos, ref, "*", "*", "*", "*", genotype]))

chemovariants[key] = [key, chrom, pos, ref, "*", "*", "*", "*", genotype]

outfile.write('\t'.join([key, chrom, pos, ref, "*", "*", "*", "*", genotype])+'\n')

elif len(res) == 1: #该化疗位点有检出

line = res[0].strip().split('\t')

chr, start, _, ref, alt, depth, ad, freq = line[0:8]

if float(

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值