我正在尝试省略以“KO”开头的行,但是当我运行代码时,行仍然被写入输出文件。我试着调用一个布尔表达式来查看“KO”是否在geneData中并且它返回为真。我只是坚持那部分。
#Read in hsa links
hsa = []
with open ('/users/skylake/desktop/pathway-HSAs.txt', 'r') as file:
for line in file:
line = line.strip()
hsa.append(line)
#Import Modules | Create KEGG Variable
from bioservices.kegg import KEGG
import re
k = KEGG()
##Data Parsing | Writing to File
#for i in range(len(hsa)):
data = k.get(hsa[2])
dict_data = k.parse(data)
#Prep title of file
nameData = re.sub("\[u'", "", str(dict_data['NAME']))
nameData = re.sub(" - Homo sapiens(human)']", "", nameData)
f = open('/Users/Skylake/Desktop/pathway-info/' + nameData + '.txt' , 'w')
#Prep gene data format
geneData = re.sub("', u'", "',\n", str(dict_data['GENE']))
geneData = re.sub("': u'", ": ", geneData)
geneData = re.sub("{u'", "", geneData)
geneData = re.sub("'}", "", geneData)
geneData = re.sub("\[KO", "\nKO", geneData)
f.write("Genes\n")
f.writelines([line for line in geneData if 'KO' not in line])
#Prep compound data format
if 'COMPOUND' in dict_data:
compData = re.sub("\"", "'", str(dict_data['COMPOUND']))
compData = re.sub("', u'", "\n", compData)
compData = re.sub("': u'", ": ", compData)
compData = re.sub("{u'", "", compData)
compData = re.sub("'}", "", compData)
f.write("\nCompounds\n")
f.write(compData)
#Close file
f.close()
答案 0 :(得分:0)
您的genedata
变量是一个字符串。当你迭代它时,你正在处理字符串的各个字符;您的line
变量可疑名称错误。两个字符的字符串' KO'显然不包含在任何这些单个字符中,因此您的布尔条件始终为True。
由于没有示例输入数据,也没有任何预期的输出数据,我无法告诉您尝试做什么,以建议解决方案。