我的输入是这样的:的Python:抓住整个字符串作为一个元素
MSG1 .STRINGZ “This is my sample string : "
MEMORYSPACE .BLKW 9
NEWLINE .FILL #10
NEG48 .FILl #-48
.END
现在我有,通过字将每个行我输入文件中像这样的代码:
['MSG1', '.STRINGZ', '"This', 'is', 'a' , 'sample' , 'string','"']
['MEMORYSPACE', '.BLKW', '9']
['NEWLINE', '.FILL', '#10']
['NEG48', '.FILl', '#-48']
[]
['.END']
在输入文件中,在我的第一行我有字符串,我希望它把整个字符串当作一个元素,这样我就可以在我的代码中计算它的长度。有没有办法做到这一点?这里是我的代码:
f = open ('testLC31.txt', 'r')
line_count = 0
to_ignore = ["AND", "ADD", "LEA", "PUTS", "JSR", "LD", "JSRR" , "NOT", "LDI" ,
"LDR", "ST", "STI", "STR", "BR" , "JMP", "TRAP" , "JMP", "RTI" ,
"BR", "ST", "STI" , "STR" , "BRz", "BRn" , "HALT"]
label = []
instructions = []
for line in f:
elem = line.split() if line.split() else ['']
if len(elem) > 1 and elem[0] not in to_ignore:
label.append(elem[0])
instructions.append(elem[1])
line_count += 1
elif elem[0] in to_ignore:
line_count += 1
是分隔符制表,空格运行或组合方式? – 2015-02-11 03:00:24