gpt4 book ai didi

python - 在 python 中过滤 CSV 文件

转载 作者:太空宇宙 更新时间:2023-11-04 10:51:16 24 4
gpt4 key购买 nike

我已经下载了这个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 - 在 python 中过滤 CSV 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13713752/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com