我有一個相當難的問題,我只是無法修復.. 這個想法是循環瀏覽部分數據並找到任何縮進。 (總是空格) 每一行的縮進比前一個更大,例如4個空格,第一行應該是字典的關鍵字,並且應該附加下一個值。通過數據循環嵌套字典
如果還有另一個縮進,這意味着應該創建一個帶有鍵和值的新字典。這應該發生遞歸,直到通過數據。 爲了讓事情更容易理解我做了一個例子:
Chassis 1:
Servers:
Server 1/1:
Equipped Product Name: EEE UCS B200 M3
Equiped PID: e63-samp-33
Equipped VID: V01
Acknowledged Cores: 16
Acknowledged Adapters: 1
PSU 1:
Presence: Equipped
VID: V00
HW Revision: 0
的想法是能夠得到在字典的形式返回數據的任何部分。 dictionary.get(「Chassis 1:」)應該返回所有數據,dictionary.get(「Servers」)應該返回比「Servers」行更深地縮進的所有內容。字典.get(「PSU 1:」)應該給出{「PSU 1:」:「Presence:Equipped」,「VID:100」,「HW Revision:0」}等等。 我已經繪製了一個小計劃來證明這一點,每種顏色是另一個字典。
當縮進再次變得較小時,例如從8到4個空格時,應將數據附加到數據較少縮進的字典中。
我已經把它在代碼試圖,但它不來接近我想要的任何地方..
for item in Array:
regexpatt = re.search(":$", item)
if regexpatt:
keyFound = True
break
if not keyFound:
return Array
#Verify if we still have lines with spaces
spaceFound = False
for item in Array:
if item != item.lstrip():
spaceFound = True
break
if not spaceFound:
return Array
keyFound = False
key=""
counter = -1
for item in Array:
counter += 1
valueTrim = item.lstrip()
valueL = len(item)
valueTrimL = len(valueTrim)
diff = (valueL - valueTrimL)
nextSame = False
if item in Array:
nextValue = Array[counter]
nextDiff = (len(nextValue) - len(nextValue.lstrip()))
if diff == nextDiff:
nextSame = True
if diff == 0 and valueTrim != "" and nextSame is True:
match = re.search(":$", item)
if match:
key = item
newArray[key] = []
deptDetermine = True
keyFound = True
elif diff == 0 and valueTrim != "" and keyFound is False:
newArray["0"].append(item)
elif valueTrim != "":
if depthDetermine:
depth = diff
deptDetermine = False
#newValue = item[-valueL +depth]
item = item.lstrip().rstrip()
newArray[key].append(item)
for item in newArray:
if item != "0":
newArray[key] = newArray[key]
return newArray
結果應該是這樣的,例如:
{
"Chassis 1": {
"PSU 1": {
"HW Revision: 0",
"Presence: Equipped",
"VID: V00"
},
"Servers": {
"Server 1/1": {
"Acknowledged Adapters: 1",
"Acknowledged Cores: 16",
"Equiped PID: e63-samp-33",
"Equipped Product Name: EEE UCS B200 M3",
"Equipped VID: V01"
}
}
}
}
我希望這解釋了足夠的概念
那麼你的代碼是做什麼的,哪些做得不夠精確? – jonrsharpe
您需要實現一個下推式自動機。 –
jonrsharpe它製作了一個帶有鍵/列表的字典,但沒有正確的格式或順序,所以我選擇不包含'結果',因爲它包含相當多的缺陷.. @JoelCornett任何示例或答案?隨意張貼一些東西! – Yenthe