漂亮地打印具有指定宽度的多个键值对
Pretty print multiple key value pairs with a specified width
我有下面的结构,我想把它写入一个指定宽度的文件,有趣的是我无法让它工作。
{'c': {' eva X': -15,
'Cki Xi': -2,
'I Xalt': -23,
'Ik Xip': -8,
'Ir 1 X': -19,
'Xamdal': 20,
'Xincik': 11,
'bu aXa': -1,
'elinde Xop': -24,
'gol aX': 5,
'huyu X': -6,
'ie Xol': -14,
'im teX': -16,
'k Xipl': 18,
'kriz X': 17,
'm Xars': 7,
'mUS aX': -13,
'mem Xi': -21,
'na Xog': 3,
'ncu X ': 9,
'ram Xo': 4,
'tI Xat': 22,
'vre aX': 12,
'zay Xo': 10}}
结果应如下所示,(ps:顺序可能不同)
{'c': {"bu aXa": -1, "Cki Xi": -2, "na Xog": 3,
"ram Xo": 4, "gol aX": 5, "huyu X": -6,
"m Xars": 7, "Ik Xip": -8, "ncu X ": 9,
"zay Xo": 10, "Xincik": 11, "vre aX": 12,
"mUS aX": -13, "ie Xol": -14, " eva X": -15,
"im teX": -16, "kriz X": 17, "k Xipl": 18,
"Ir 1 X": -19, "Xamdal": 20, "mem Xi": -21,
"tI Xat": 22, "I Xalt": -23, "elinde Xop": -24}}
我已尝试 pprint
如下,但无济于事。我得不到想要的结果。
import pprint
pprint.pprint(data, width=100)
pprint.pprint(data, width=200)
pprint.pprint(data, width=300)
# {'c': {' eva X': -15,
# 'Cki Xi': -2,
# 'I Xalt': -23,
# 'Ik Xip': -8,
# 'Ir 1 X': -19,
# 'Xamdal': 20... (all of them same as above)
在 pprint 的文档中说 -
The desired output width is constrained using the width parameter; the default is 80 characters. If a structure cannot be formatted within the constrained width, a best effort will be made. If compact is false (the default) each item of a long sequence will be formatted on a separate line. If compact is true, as many items as will fit within the width will be formatted on each output line.
所以您可以使用 compact=True
参数来获得您想要的输出。但是你需要找到像你预期的那样在一行中压缩三个 key:value 的大约宽度。
这是我建议的使用纯 Python 的解决方案。包含评论是为了帮助理解我的逻辑:
indict = {'c': {' eva X': -15,
'Cki Xi': -2,
'I Xalt': -23,
'Ik Xip': -8,
'Ir 1 X': -19,
'Xamdal': 20,
'Xincik': 11,
'bu aXa': -1,
'elinde Xop': -24,
'gol aX': 5,
'huyu X': -6,
'ie Xol': -14,
'im teX': -16,
'k Xipl': 18,
'kriz X': 17,
'm Xars': 7,
'mUS aX': -13,
'mem Xi': -21,
'na Xog': 3,
'ncu X ': 9,
'ram Xo': 4,
'tI Xat': 22,
'vre aX': 12,
'zay Xo': 10}}
#Create a string from dict.
dictString = str(indict)
#Set the number of elements you want for each row.
elements = 3
#Split the string to individual records of dict.
dictString = dictString.split(',')
#Add ' ,' to the end.
newString = list(map(lambda i: i+" ,", dictString[:-1]))
newString.append(dictString[-1])
#Find the position of the dict 'opening' bracket in order to be used as padding point.
pos = newString[0].find(": {") + 2
print(pos)
#Form row with number of elements defined by the 'elements' variable.
newString = ["".join(newString[i:i+elements]) for i in range(0,len(newString),elements)]
#Pad each row to match with the padding point.
newString = [newString[0]] + [i.rjust(len(i) + pos,' ') for i in newString[1:]]
#Create a final string, appending all rows.
finalString = "\n".join(newString)
print(finalString)
#Write the string to file.
with open("output.txt", "w") as outFile:
outFile.write(finalString)
来自print(finalString)
的输出:
{'c': {' eva X': -15 , 'Cki Xi': -2 , 'I Xalt': -23 ,
'Ik Xip': -8 , 'Ir 1 X': -19 , 'Xamdal': 20 ,
'Xincik': 11 , 'bu aXa': -1 , 'elinde Xop': -24 ,
'gol aX': 5 , 'huyu X': -6 , 'ie Xol': -14 ,
'im teX': -16 , 'k Xipl': 18 , 'kriz X': 17 ,
'm Xars': 7 , 'mUS aX': -13 , 'mem Xi': -21 ,
'na Xog': 3 , 'ncu X ': 9 , 'ram Xo': 4 ,
'tI Xat': 22 , 'vre aX': 12 , 'zay Xo': 10}}
文件内容也是如此。
这是我针对自己的问题编写的解决方案,根据我的需要稍作修改。我仍然对想法持开放态度。如果 pprint
涵盖这种情况会更好,不幸的是。
def prettyPrint(myDict, itemPerLine=3):
myStr = '{'
for k1, v1 in myDict.items():
myStr += "'" + k1 + "': {"
itemCount = 0
for k2, v2 in v1.items():
myStr += '"' + str(k2) + '": ' + str(v2) + ', '
if itemCount == itemPerLine:
myStr += '\n' + ' ' * 32
itemCount = 0
itemCount += 1
myStr = myStr[:-2] + "},\n" + ' ' * 26
return myStr[:-2] + "}"
prettyPrint(data)
{'c': {"bu aXa": -1, "Cki Xi": -2, "na Xog": 3,
"ram Xo": 4, "gol aX": 5, "huyu X": -6,
"m Xars": 7, "Ik Xip": -8, "ncu X ": 9,
"zay Xo": 10, "Xincik": 11, "vre aX": 12,
"mUS aX": -13, "ie Xol": -14, " eva X": -15,
"im teX": -16, "kriz X": 17, "k Xipl": 18,
"Ir 1 X": -19, "Xamdal": 20, "mem Xi": -21,
"tI Xat": 22, "I Xalt": -23, "elinde Xop": -24}}
我有下面的结构,我想把它写入一个指定宽度的文件,有趣的是我无法让它工作。
{'c': {' eva X': -15,
'Cki Xi': -2,
'I Xalt': -23,
'Ik Xip': -8,
'Ir 1 X': -19,
'Xamdal': 20,
'Xincik': 11,
'bu aXa': -1,
'elinde Xop': -24,
'gol aX': 5,
'huyu X': -6,
'ie Xol': -14,
'im teX': -16,
'k Xipl': 18,
'kriz X': 17,
'm Xars': 7,
'mUS aX': -13,
'mem Xi': -21,
'na Xog': 3,
'ncu X ': 9,
'ram Xo': 4,
'tI Xat': 22,
'vre aX': 12,
'zay Xo': 10}}
结果应如下所示,(ps:顺序可能不同)
{'c': {"bu aXa": -1, "Cki Xi": -2, "na Xog": 3,
"ram Xo": 4, "gol aX": 5, "huyu X": -6,
"m Xars": 7, "Ik Xip": -8, "ncu X ": 9,
"zay Xo": 10, "Xincik": 11, "vre aX": 12,
"mUS aX": -13, "ie Xol": -14, " eva X": -15,
"im teX": -16, "kriz X": 17, "k Xipl": 18,
"Ir 1 X": -19, "Xamdal": 20, "mem Xi": -21,
"tI Xat": 22, "I Xalt": -23, "elinde Xop": -24}}
我已尝试 pprint
如下,但无济于事。我得不到想要的结果。
import pprint
pprint.pprint(data, width=100)
pprint.pprint(data, width=200)
pprint.pprint(data, width=300)
# {'c': {' eva X': -15,
# 'Cki Xi': -2,
# 'I Xalt': -23,
# 'Ik Xip': -8,
# 'Ir 1 X': -19,
# 'Xamdal': 20... (all of them same as above)
在 pprint 的文档中说 -
The desired output width is constrained using the width parameter; the default is 80 characters. If a structure cannot be formatted within the constrained width, a best effort will be made. If compact is false (the default) each item of a long sequence will be formatted on a separate line. If compact is true, as many items as will fit within the width will be formatted on each output line.
所以您可以使用 compact=True
参数来获得您想要的输出。但是你需要找到像你预期的那样在一行中压缩三个 key:value 的大约宽度。
这是我建议的使用纯 Python 的解决方案。包含评论是为了帮助理解我的逻辑:
indict = {'c': {' eva X': -15,
'Cki Xi': -2,
'I Xalt': -23,
'Ik Xip': -8,
'Ir 1 X': -19,
'Xamdal': 20,
'Xincik': 11,
'bu aXa': -1,
'elinde Xop': -24,
'gol aX': 5,
'huyu X': -6,
'ie Xol': -14,
'im teX': -16,
'k Xipl': 18,
'kriz X': 17,
'm Xars': 7,
'mUS aX': -13,
'mem Xi': -21,
'na Xog': 3,
'ncu X ': 9,
'ram Xo': 4,
'tI Xat': 22,
'vre aX': 12,
'zay Xo': 10}}
#Create a string from dict.
dictString = str(indict)
#Set the number of elements you want for each row.
elements = 3
#Split the string to individual records of dict.
dictString = dictString.split(',')
#Add ' ,' to the end.
newString = list(map(lambda i: i+" ,", dictString[:-1]))
newString.append(dictString[-1])
#Find the position of the dict 'opening' bracket in order to be used as padding point.
pos = newString[0].find(": {") + 2
print(pos)
#Form row with number of elements defined by the 'elements' variable.
newString = ["".join(newString[i:i+elements]) for i in range(0,len(newString),elements)]
#Pad each row to match with the padding point.
newString = [newString[0]] + [i.rjust(len(i) + pos,' ') for i in newString[1:]]
#Create a final string, appending all rows.
finalString = "\n".join(newString)
print(finalString)
#Write the string to file.
with open("output.txt", "w") as outFile:
outFile.write(finalString)
来自print(finalString)
的输出:
{'c': {' eva X': -15 , 'Cki Xi': -2 , 'I Xalt': -23 ,
'Ik Xip': -8 , 'Ir 1 X': -19 , 'Xamdal': 20 ,
'Xincik': 11 , 'bu aXa': -1 , 'elinde Xop': -24 ,
'gol aX': 5 , 'huyu X': -6 , 'ie Xol': -14 ,
'im teX': -16 , 'k Xipl': 18 , 'kriz X': 17 ,
'm Xars': 7 , 'mUS aX': -13 , 'mem Xi': -21 ,
'na Xog': 3 , 'ncu X ': 9 , 'ram Xo': 4 ,
'tI Xat': 22 , 'vre aX': 12 , 'zay Xo': 10}}
文件内容也是如此。
这是我针对自己的问题编写的解决方案,根据我的需要稍作修改。我仍然对想法持开放态度。如果 pprint
涵盖这种情况会更好,不幸的是。
def prettyPrint(myDict, itemPerLine=3):
myStr = '{'
for k1, v1 in myDict.items():
myStr += "'" + k1 + "': {"
itemCount = 0
for k2, v2 in v1.items():
myStr += '"' + str(k2) + '": ' + str(v2) + ', '
if itemCount == itemPerLine:
myStr += '\n' + ' ' * 32
itemCount = 0
itemCount += 1
myStr = myStr[:-2] + "},\n" + ' ' * 26
return myStr[:-2] + "}"
prettyPrint(data)
{'c': {"bu aXa": -1, "Cki Xi": -2, "na Xog": 3,
"ram Xo": 4, "gol aX": 5, "huyu X": -6,
"m Xars": 7, "Ik Xip": -8, "ncu X ": 9,
"zay Xo": 10, "Xincik": 11, "vre aX": 12,
"mUS aX": -13, "ie Xol": -14, " eva X": -15,
"im teX": -16, "kriz X": 17, "k Xipl": 18,
"Ir 1 X": -19, "Xamdal": 20, "mem Xi": -21,
"tI Xat": 22, "I Xalt": -23, "elinde Xop": -24}}