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

yipeiwu_com6年前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设计】。

相关文章

对python3标准库httpclient的使用详解

如下所示: import http.client, urllib.parse import http.client, urllib.parse import random USER...

致Python初学者 Anaconda入门使用指南完整版

打算学习 Python 来做数据分析的你,是不是在开始时就遇到各种麻烦呢? 到底该装 Python2 呢还是 Python3 ? 为什么安装 Python 时总是出错? 怎么安装工具包呢...

python中获得当前目录和上级目录的实现方法

获取当前文件的路径: from os import path d = path.dirname(__file__) #返回当前文件所在的目录 # __file__ 为当前文件...

windows下Virtualenvwrapper安装教程

windows下Virtualenvwrapper安装教程

windows下安装Virtualenvwrapper 我们可以使用Virtualenvwrapper来方便地管理python虚拟环境,但是在windows上安装的时候.....直接 i...

python列出目录下指定文件与子目录的方法

本文实例讲述了python列出目录下指定文件与子目录的方法。分享给大家供大家参考。具体实现方法如下: # if you know the exact name: import os...