Python 编译和迭代字典
我对python相当陌生,正在从一个文件构建一个字典,然后迭代字典。我一直在eclipse中工作,没有收到任何输出,甚至没有收到任何警告 输入如下所示(实际输入显著增大) 我试图产生的结果是Python 编译和迭代字典,python,dictionary,Python,Dictionary,我对python相当陌生,正在从一个文件构建一个字典,然后迭代字典。我一直在eclipse中工作,没有收到任何输出,甚至没有收到任何警告 输入如下所示(实际输入显著增大) 我试图产生的结果是 GO:0000010 molecular_function trans-hexaprenyltranstransferase activity GO:0016765 ! transferase activity, transferring alkyl or aryl (other than meth
GO:0000010 molecular_function
trans-hexaprenyltranstransferase activity
GO:0016765 ! transferase activity, transferring alkyl or aryl (other than methyl) groups
GO:0000011 biological_process
vacuole inheritance
is_a: GO:0007033 ! vacuole organization
is_a: GO:0048308 ! organelle inheritance
GO:0000012 biological_process
single strand break repair
is_a: GO:0006281 ! DNA repair
GO:0000014 molecular_function
single-stranded DNA endodeoxyribonuclease activity
is_a: GO:0004520 ! endodeoxyribonuclease activity
我的密码是:
import re
id_to_info = {} #declare dictionary
def parse_record(term):
go_id = re.findall(r"id:\s(.*?)\n", term, re.DOTALL)
name = re.findall(r"name:\s(.*?)\n", term, re.DOTALL)
namespace = re.findall(r"namespace:\s(.*?)\n", term, re.DOTALL)
is_a = re.findall(r"is_a:\s(.*?)\n", term, re.DOTALL)
info = namespace + "\n" + name + "\n" + is_a
id_to_info[go_id] = info
for go_id, info in id_to_info.interitems():
print(go_id + "\t" + info)
def split_record(record):
sp_file = open(record)
sp_records = sp_file.read()
sp_split_records = re.findall(r"(\[.*?)\n\n", sp_records, re.DOTALL)
for sp_record in sp_split_records:
parse_record(term=sp_record)
sp_file.close()
split_record(record="go.rtf")
我真的不知道我错在哪里,但我想主要的问题是我的字典调用
import re
id_to_info = {} #declare dictionary
def parse_record(term):
go_id = re.findall(r"id:\s(.*?)\n", term, re.DOTALL)[0]
name = re.findall(r"name:\s(.*?)\n", term, re.DOTALL)[0]
namespace = re.findall(r"namespace:\s(.*?)\n", term, re.DOTALL)[0]
is_a = re.findall(r'is_a:(.*)', term, re.DOTALL)[0]
info = namespace + "\n" + name + "\n" + is_a
id_to_info[go_id] = info
for go_id, info in id_to_info.iteritems():
print(go_id + "\t" + info)
def split_record(record):
sp_file = open(record)
sp_records = sp_file.read()
sp_split_records = re.findall(r"(\[.*?)\n\n", sp_records, re.DOTALL)
for sp_record in sp_split_records:
parse_record(term=sp_record)
sp_file.close()
split_record(record="go.rtf")
我建议不要使用IDE,而是使用终端或
至少要调试解释器:
Python 2.7.10 (default, Jul 30 2016, 18:31:42)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> s = """[Term]
... id: GO:0000010
... name: trans-hexaprenyltranstransferase activity
... namespace: molecular_function
... def: "Catalysis of the reaction: all-trans-hexaprenyl diphosphate + isopentenyl diphosphate = all-trans-heptaprenyl diphosphate + diphosphate." [KEGG:R05612, RHEA:20839]
... subset: gosubset_prok
... xref: KEGG:R05612
... xref: RHEA:20839
... is_a: GO:0016765 ! transferase activity, transferring alkyl or aryl (other than methyl) groups"""
>>> import re
>>> re.findall(r'is_a:(.*)', s)
[' GO:0016765 ! transferase activity, transferring alkyl or aryl (other than methyl) groups']
此外,Python是动态的,这意味着它不需要编译和运行。。它将一直运行到出错为止
您的问题:
1) 正则表达式-谷歌漫游
2) 打字错误!你们都可以阅读
Python文档。他们真的很好。。或者随便挑一本书。。你会学到很多
在编写代码和在解释器上进行实验时进行了大量操作
---巨蟒爱好者
我建议不要使用IDE,而是使用终端或
至少要调试解释器:
Python 2.7.10 (default, Jul 30 2016, 18:31:42)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> s = """[Term]
... id: GO:0000010
... name: trans-hexaprenyltranstransferase activity
... namespace: molecular_function
... def: "Catalysis of the reaction: all-trans-hexaprenyl diphosphate + isopentenyl diphosphate = all-trans-heptaprenyl diphosphate + diphosphate." [KEGG:R05612, RHEA:20839]
... subset: gosubset_prok
... xref: KEGG:R05612
... xref: RHEA:20839
... is_a: GO:0016765 ! transferase activity, transferring alkyl or aryl (other than methyl) groups"""
>>> import re
>>> re.findall(r'is_a:(.*)', s)
[' GO:0016765 ! transferase activity, transferring alkyl or aryl (other than methyl) groups']
此外,Python是动态的,这意味着它不需要编译和运行。。它将一直运行到出错为止
您的问题:
1) 正则表达式-谷歌漫游
2) 打字错误!你们都可以阅读
Python文档。他们真的很好。。或者随便挑一本书。。你会学到很多
在编写代码和在解释器上进行实验时进行了大量操作
---巨蟒爱好者 re.findall返回它找到的东西的列表;您的代码假定为字符串。因为每行只有一次点击,所以只要在可行的地方添加[0]is_a可以空回来,因此需要更细致的处理 此外,(键,值)方法是iteritems(迭代项),而不是iteritems 以下是最新消息:
import re
id_to_info = {} #declare dictionary
def parse_record(term):
go_id = re.findall(r"id:\s(.*?)\n", term, re.DOTALL)[0]
name = re.findall(r"name:\s(.*?)\n", term, re.DOTALL)[0]
namespace = re.findall(r"namespace:\s(.*?)\n", term, re.DOTALL)[0]
is_a = re.findall(r"is_a:\s(.*?)\n", term, re.DOTALL)
is_a = is_a[0] if is_a else ""
# print namespace, name, is_a
info = namespace + "\n" + name + "\n" + is_a
id_to_info[go_id] = info
for go_id, info in id_to_info.iteritems():
print(go_id + "\t" + info)
def split_record(record):
sp_file = open(record)
sp_records = sp_file.read()
sp_split_records = re.findall(r"(\[.*?)\n\n", sp_records, re.DOTALL)
for sp_record in sp_split_records:
parse_record(term=sp_record)
sp_file.close()
split_record(record="go.rtf")
输出:
GO:0000010 molecular_function
trans-hexaprenyltranstransferase activity
GO:0016765 ! transferase activity, transferring alkyl or aryl (other
GO:0000011 biological_process
vacuole inheritance
GO:0007033 ! vacuole organization
GO:0000010 molecular_function
trans-hexaprenyltranstransferase activity
GO:0016765 ! transferase activity, transferring alkyl or aryl (other
GO:0000011 biological_process
vacuole inheritance
GO:0007033 ! vacuole organization
GO:0000010 molecular_function
trans-hexaprenyltranstransferase activity
GO:0016765 ! transferase activity, transferring alkyl or aryl (other
GO:0000012 biological_process
single strand break repair
我将把其余的格式留给您。:-) re.findall返回它找到的东西的列表;您的代码假定为字符串。因为每行只有一次点击,所以只要在可行的地方添加[0]is_a可以空回来,因此需要更细致的处理 此外,(键,值)方法是iteritems(迭代项),而不是iteritems 以下是最新消息:
import re
id_to_info = {} #declare dictionary
def parse_record(term):
go_id = re.findall(r"id:\s(.*?)\n", term, re.DOTALL)[0]
name = re.findall(r"name:\s(.*?)\n", term, re.DOTALL)[0]
namespace = re.findall(r"namespace:\s(.*?)\n", term, re.DOTALL)[0]
is_a = re.findall(r"is_a:\s(.*?)\n", term, re.DOTALL)
is_a = is_a[0] if is_a else ""
# print namespace, name, is_a
info = namespace + "\n" + name + "\n" + is_a
id_to_info[go_id] = info
for go_id, info in id_to_info.iteritems():
print(go_id + "\t" + info)
def split_record(record):
sp_file = open(record)
sp_records = sp_file.read()
sp_split_records = re.findall(r"(\[.*?)\n\n", sp_records, re.DOTALL)
for sp_record in sp_split_records:
parse_record(term=sp_record)
sp_file.close()
split_record(record="go.rtf")
输出:
GO:0000010 molecular_function
trans-hexaprenyltranstransferase activity
GO:0016765 ! transferase activity, transferring alkyl or aryl (other
GO:0000011 biological_process
vacuole inheritance
GO:0007033 ! vacuole organization
GO:0000010 molecular_function
trans-hexaprenyltranstransferase activity
GO:0016765 ! transferase activity, transferring alkyl or aryl (other
GO:0000011 biological_process
vacuole inheritance
GO:0007033 ! vacuole organization
GO:0000010 molecular_function
trans-hexaprenyltranstransferase activity
GO:0016765 ! transferase activity, transferring alkyl or aryl (other
GO:0000012 biological_process
single strand break repair
我将把其余的格式留给您。:-)
id\u to\u info.interitems():
不会剪切它。请尝试id\u to\u info.items():
您的代码在此处为我崩溃:info=namespace+“\n”+name+“\n”+是\u a
,因为是\u a
是一个列表。。。。你实际得到了什么样的输出?如何从列表中转换is_a是最好的方法?我尝试了“”。join(是a),但它仍然没有向\u info提供任何输出或错误id\u。interitems():
不会剪切它。请尝试id\u to\u info.items():
您的代码在此处为我崩溃:info=namespace+“\n”+name+“\n”+是\u a
,因为是\u a
是一个列表。。。。你实际得到了什么样的输出?如何从列表中转换is_a是最好的方法?我尝试了“”。join(is_a),但它仍然没有给出任何输出或错误