2

我有以下结构,我想把它写到一个指定宽度的文件中,有趣的是我无法让它工作。

{'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)
4

3 回答 3

0

这是我为自己的问题编写的解决方案,根据我的需要稍作修改。我仍然对想法持开放态度。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}}
于 2018-06-29T10:32:10.693 回答
0

在 pprint 的文档中它说 -

使用 width 参数限制所需的输出宽度;默认为 80 个字符。如果无法在限制宽度内格式化结构,将尽最大努力。如果 compact 为 false(默认值),则长序列的每个项目将被格式化为单独的行。如果 compact 为真,则在每个输出行上都会格式化宽度范围内的尽可能多的项目。

因此,您可以将compact=True参数用于所需的输出。但是您需要找到压缩三个 key:value 的近似宽度,就像您预期的那样。

于 2018-06-28T11:17:06.393 回答
0

这是我建议的使用纯 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}}

文件内容也是如此。

于 2018-06-29T09:35:36.740 回答