我的輸入是這樣的:的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