在python中过滤CSV文件
作者:互联网
我已经下载了csv file,它创建了一个基因信息电子表格.重要的是在HLA- *列中有基因信息.如果基因的分辨率太低,例如DQB1 * 03,则应删除该行.如果数据分辨率过高,例如DQB1 * 03:02:01,则需要删除末尾的:01标签.因此,理想情况下,我希望蛋白质的格式为DQB1 * 03:02,以便在DQB1 *之后具有两个分辨率级别.我如何告诉python查找这些格式,而忽略存储在其中的数据.
例如
if (csvCell is of format DQB1*03:02:01):
delete the :01 # but do this in a general format
elif (csvCell is of format DQB1*03):
delete row
else:
goto next line
更新:我引用的已编辑代码
import csv
import re
import sys
csvdictreader = csv.DictReader(open('mhc.csv','r+b'), delimiter=',')
csvdictwriter = csv.DictWriter(file('mhc_fixed.csv','r+b'), fieldnames=csvdictreader.fieldnames, delimiter=',')
csvdictwriter.writeheader()
targets = [name for name in csvdictreader.fieldnames if name.startswith('HLA-D')]
for rowfields in csvdictreader:
keep = True
for field in targets:
value = rowfields[field]
if re.match(r'^\w+\*\d\d$', value):
keep = False
break # quit processing target fields
elif re.match(r'^(\w+)\*(\d+):(\d+):(\d+):(\d+)$', value):
rowfields[field] = re.sub(r'^(\w+)\*(\d+):(\d+):(\d+):(\d+)$',r'\1*\2:\3', value)
else: # reduce gene resolution if too high
# by only keeping first two alles if three are present
rowfields[field] = re.sub(r'^(\w+)\*(\d+):(\d+):(\d+)$',r'\1*\2:\3', value)
if keep:
csvdictwriter.writerow(rowfields)
解决方法:
我认为这可以满足您的需求.这并不像Peter的回答那么简单,因为它使用Python的csv模块来处理文件.可以将其重写和简化为仅像对待文件一样将其视为纯文本,但这应该很容易.
import csv
import re
import sys
csvdictreader = csv.DictReader(sys.stdin, delimiter=',')
csvdictwriter = csv.DictWriter(sys.stdout, fieldnames=csvdictreader.fieldnames, delimiter=',')
csvdictwriter.writeheader()
targets = [name for name in csvdictreader.fieldnames if name.startswith('HLA-')]
for rowfields in csvdictreader:
keep = True
for field in targets:
value = rowfields[field]
if re.match(r'^DQB1\*\d\d$', value): # gene resolution too low?
keep = False
break # quit processing target fields
else: # reduce gene resolution if too high
# by only keeping first two alles if three are present
rowfields[field] = re.sub(r'^DQB1\*(\d\d):(\d\d):(\d\d)$',
r'DQB1*\1:\2', value)
if keep:
csvdictwriter.writerow(rowfields)
对我而言,最难的部分是确定您想做什么.
标签:python,csv,formatting,bioinformatics 来源: https://codeday.me/bug/20191009/1881463.html