python 提取文件指定列的方法示例

yipeiwu_com7年前Python基础

之前用featureCount 处理得到结果,要提出第一列gene_id 和 readcount 列,首先软件输出的第一行默认是你使用的命令行,没有用,用bash批量删掉。

for i in `ls`;do sed -i '1d' $i;done

删除当前文件夹下所有文件第一行。

其实提出两列很简单,不过我受够了每次一个文件执行一次的烦。想搞成别的程序调用时命令行参数直接就行。第一次知道sys.argv这玩意,学到了。

我设置了 -i 输入,-o 输出 这两个参数来判断输入输出文件个数,不过对于错误命令行输入的判断还很欠缺,毕竟只有自己用。

上代码:

import sys
leng=len(sys.argv)
for i in range(leng): # index the input file and output file location,maybe it's too complex
 if sys.argv[i]=="-i":
 it=i
 if sys.argv[i]=="-o":
 out=i
for i in range(it+1,out): # input file number(there should have same # of in and out file)
 print(sys.argv[i])
 file=open(sys.argv[i],'r')
 f=open(sys.argv[i+out-it],'w')
 count=[]
 for lines in file.readlines():
 two=[]
 lines=lines.rstrip('\n')
 b=lines.split('\t')
 two.append(b[0])
 if(b[6].startswith('.')): #chage path to count,some output problem
  b[6]="count"
 two.append(b[6])
 s='\t'.join(two)
 f.write(s+'\n')
 f.close

其实python里面 str 和list 弄的还是昏头昏脑的,每次都要错了再改。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python连接mysql实例分享

示例一 #coding=UTF-8 import sys import MySQLdb import time reload(sys) sys.setdefaultencodin...

解决webdriver.Chrome()报错:Message:'chromedriver' executable needs to be in Path

解决webdriver.Chrome()报错:Message:'chromedriver' executable needs to be in Path

'chromedriver' executable needs to be in Path 声明:本人萌新,刚学python不久记录一下自己的坑,发出来若能帮助到一些人尽早解决问题那便是...

Python入门Anaconda和Pycharm的安装和配置详解

Python入门Anaconda和Pycharm的安装和配置详解

子曰:“工欲善其事,必先利其器。”学习Python就需要有编译Python程序的软件,一般情况下,我们选择在Python官网下载对应版本的Python然后用记事本编写,再在终端进行编译运...

Python3标准库总结

Python3标准库 操作系统接口 os模块提供了不少与操作系统相关联的函数。 >>> import os >>> os.getcwd()...

import的本质解析

上一篇文章中我们介绍了python中hashlib模块用法示例,详情参见:/post/127103.htm。今天我们看看import的有关内容。 编程时总是用到import导入,动不动就...