我在excel中有一个csv文件,它包含BLAST搜索的输出,格式如下:
# BLASTN 2.2.29+
# Query: Cryptocephalus androgyne
# Database: SANdouble
# Fields: query id subject id % identity alignment length mismatches gap opens q. start q. end s. start s. end evalue bit score
# 1 hits found
Cryptocephalus ctg7180000094003 79.59 637 110 9 38 655 1300 1935 1.00E-125 444
# BLASTN 2.2.29+
# Query: Cryptocephalus aureolus
# Database: SANdouble
# Fields: query id subject id % identity alignment length mismatches gap opens q. start q. end s. start s. end evalue bit score
# 4 hits found
Cryptocephalus ctg7180000093816 95.5 667 12 8 7 655 1269 1935 0 1051
Cryptocephalus ctg7180000094021 88.01 667 62 8 7 655 1269 1935 0 780
Cryptocephalus ctg7180000094015 81.26 667 105 13 7 654 1269 1934 2.00E-152 532
Cryptocephalus ctg7180000093818 78.64 515 106 4 8 519 1270 1783 2.00E-94 340
我已经使用
将此作为csv导入到python中with open('BLASToutput.csv', 'rU') as csvfile:
contents = csv.reader(csvfile, delimiter=' ', quotechar='|')
for row in contents:
table = ', '.join(row)
我现在想要做的是将列数据提取出来。我的总体目标是计算所有具有超过98%身份的比赛(第三栏)。
问题在于,由于这不是典型的csv格式,因此顶部没有标题,因此我无法根据其标题提取列。我在想是否可以提取第三列作为列表然后我可以使用python中的常规列表工具来提取我想要的数字但是我从未使用过pythons csv模块而且我正在努力寻找合适的命令。关于SO的其他问题是类似的,但是没有提到我没有标题和空单元格的特定情况。如果你能帮助我,我将非常感激!
答案 0 :(得分:1)
数据文件与CSV格式不同。它有注释,它的分隔符不是单个字符,而是格式化的空格。
因为你的总体目标是
计算所有具有超过98%身份的匹配(第三列)。
并且数据文件内容格式正确,您可以使用普通文件解析方法:
import re
with open('BLASToutput.csv') as f:
# read the file line by line
for line in f:
# skip comments (or maybe leave as it is)
if line.startswith('#'):
# print line
continue
# split fields
fields = re.split(r' +', line)
# check if the 3rd field is greater than 98%
if float(fields[2]) > 98:
# output the matched line
print line
答案 1 :(得分:0)
我设法找到了一种方法:
Python: split files using mutliple split delimiters
import csv
csvfile = open("SANDoubleSuperMatrix.csv", "rU")
dialect = csv.Sniffer().sniff(csvfile.read(1024))
csvfile.seek(0)
reader = csv.reader(csvfile, dialect)
identity = []
for line in reader:
identity.append(line[2])
print identity