我正在使用名为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
有没有更好的方法来访问元组中的字符串?我能找到的所有资源只包括一个元组列表,并没有提到我在这里做了什么。
答案 0 :(得分:2)
当tup得到E&#39; E&#39;价值,你试图获得一个不存在的指数。
for row in numbering:
for tup in row:
if tup[1] != ' ': # Raised exception --> 'E'[1]
如果我理解你的目标,请尝试使用:
DATA = [((1, ' '), 'E'), ((2, ' '), 'V'), ((113, ' '), 'A')]
def get_tuples(data):
for item in data:
for element in item:
if isinstance(element, tuple):
yield element
else:
continue
for tup in get_tuples(DATA):
print(tup)
输出
(1, ' ')
(2, ' ')
(113, ' ')