python字典格式要求 Python字典格式

2021-10-19 21:08:53 字數 776 閱讀 7630

我做了乙個python函式來將字典轉換成格式化的字串。我的目標是讓乙個函式接受乙個字典作為輸入,並將其轉換為乙個看起來不錯的字串。例如,類似}的內容將變成:text:

hello

blah:

hi:hello

hello:

hi這是我寫的**:indent = 0

def format_dict(d):

global indent

res = ""

for key in d:

res += (" " * indent) + key + ":\n"

if not type(d[key]) == type({}):

res += (" " * (indent + 1)) + d[key] + "\n"

else:

indent += 1

res += format_dict(d[key])

indent -= 1

return res

#test

print format_dict(}})

它就像乙個符咒。它檢查字典的項是否是另乙個字典,在這種情況下,它將處理該項或其他內容,然後將其用作值。問題是:我不能把字典和字串放在一起。例如,如果我想:blah:

hihello:

hello again

沒辦法了。有沒有辦法讓我在字典裡找到乙個類似於列表的條目。像這樣的東西}?如果你能提供乙個解決方案,你能告訴我如何更改我的**(如果它確實需要更改的話)。

注意:我正在使用python2.5

python字典格式要求 Python字典格式

我建立了乙個 python函式來將字典轉換為格式化字串.我的目標是讓乙個函式接受字典輸入並將其轉換為乙個看起來不錯的字串.例如,之類的內容將變為 text hello blah hi hello hello hi這是我寫的 indent 0 def format dict d global inde...

python 語法格式要求

問題 乙個python指令碼,本來都執行好好的,然後寫了幾行 而且也都確保每行都對齊了,但是執行的時候,卻出現語法錯誤 indentationerror unindent does not match any outer indentation level 解決過程 1.對於此錯誤,最常見的原因是,...

公文規範格式要求

一般使用3號仿宋字型,版心尺寸為156mmx225mm,公文用紙上白邊為37mm 1mm 左白邊為28mm 1 每頁22行,每行28個字,行間距一般為3號漢字高度的7 8。2號小標宋體字,分一行或多行需居中排列 回行時,做到此頁完整,排列對稱,長短適宜,間距恰當 多行標題排列應當使用正梯形 倒梯形或...