我有以下代碼:rstrip不工作(Python 2.7版)
file = open("file", "r")
array = file.readlines()
stats = [1, 1, 1, 1, 1] # creating an array to fill
print array
sh1 = array[1] # breaking the array extracted from the text file up for editing
sh2 = array[2]
sh3 = array[3]
sh4 = array[4]
stats[0] = string.rstrip(sh1[1])
stats[1] = string.rstrip(sh2[1])
stats[2] = string.rstrip(sh3[1])
stats[3] = string.rstrip(sh4[1])
print stats
我期待它剝去從文本文件中提取的陣列的新行和新的數據放置到一個單獨的數組。相反,我正在從變量的任何一端刪除看似隨機數量的字符。請有人解釋我做錯了什麼?
'string'從哪裏來? –
你爲什麼只用1來填充你的數組,以便以後替換它們? –
你有目的地從'1'開始索引嗎?請記住,Python中的列表索引是從零開始的。 – werkritter