我正在使用一个名为ANARCI的工具对抗体序列进行编号,程序的输出如下所示:
[((1, ' '), 'E'), ((2, ' '), 'V'), ..., ((113, ' '), 'A')]
我试图将编号保存在.csv
文件中,但在访问上面简短部分中显示的空字符串时遇到问题。其中一些会有一个字母,我需要检查字符串是否为空。这是我为此编写的代码:
with open(fileName + '.csv', 'wb') as myFile:
# sets wr to writer function in csv module
wr = csv.writer(myFile, quoting=csv.QUOTE_ALL)
# writes in headers
wr.writerow(headers)
# iterates through numbering
for row in numbering:
# gets to the innermost tuple
for tup in row:
# checks if the string contains a letter
if tup[1] != ' ':
# makes the number and letter a single string
numScheme = str(tup[0])+str(tup[1])
# creates a list of values to write
varList = [baNumbering,numScheme,row[1]]
wr.writerow(varList)
else:
# if the string does not contain a letter, the list of values is created without the string
varList = [baNumbering,tup[0],row[1]]
wr.writerow(varList)
baNumbering = baNumbering + 1
我的想法是for row in numbering:
将我带到包含元组的元组,for tup in row:
将允许我检查最里面元组的索引。我想让varList
成为一个包含数字的列表,编号(可能带有字母),然后是字母——比如:["1","1","E"]
或["30","29B","Q"]
。但是,我得到了一个错误:
Traceback (most recent call last):
File "NumberingScript.py", line 101, in <module>
Control()
File "NumberingScript.py", line 94, in Control
Num()
File "NumberingScript.py", line 86, in Num
SaveNumbering(numbering,numberingScheme)
File "NumberingScript.py", line 72, in SaveNumbering
WriteFile(numbering,numberingScheme,fileName)
File "NumberingScript.py", line 51, in WriteFile
if tup[1] != ' ':
IndexError: string index out of range
有没有更好的方法访问元组中的字符串?我所能找到的所有资源都只包含一个元组列表,并且没有提到如何处理这里的内容。你知道吗
当tup获取“E”值并且您试图获取一个不存在的索引时,会引发异常。你知道吗
如果我正确理解你的目标,试着用这个:
输出
相关问题 更多 >
编程相关推荐