简体   繁体   English

在 json 文件中的 python 列表的每个元素之后添加换行符

[英]Adding a linebreak after every element of python list in json file

I've got a json file which contains different 2 layered lists.我有一个 json 文件,其中包含不同的 2 个分层列表。 After every element of the main list, I add a linebreak so you can easily read the data.在主列表的每个元素之后,我添加了一个换行符,以便您可以轻松读取数据。 But when I load the file and make changes to some of the elements in the lists and dump it back in, the indentation is messed up and you can't read it anymore.So how can I make it so that there's a line break after every element of the first layer of the list like before and not after every single element of the second layer list?但是当我加载文件并对列表中的某些元素进行更改并将其转储回时,缩进被搞砸了,你无法再阅读它了。那么我怎样才能让它之后有一个换行符列表第一层的每个元素都像之前一样,而不是第二层列表的每个元素之后? Thanks for helping me!谢谢你帮助我!

My code:我的代码:

with open("./world_data.json", "r") as f:
    data = json.load(f)
    new_data = data[level_name] #Accessing the right variable in the json ("level_1_data/"level_2_data")
    new_data[button[1][3][1]][button[1][3][0]] = world.block_number #Making changes to the lists 
    data[level_name] = new_data
    data.update(data)
with open("./world_data.json", "w") as f:
    json.dump(data, f, indent=4)

Before dumping (the way I want it to look after dumping):倾倒之前(我希望它在倾倒后的样子):

{
  "level_1_data": [
      [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
      [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
      [1, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 1],
      [1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 2, 2, 1]
      ],

  "level_2_data": [
      [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
      [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
      [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
      [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
      [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
}

After dumping (I left most of the data out because it would be too long):转储后(我留下了大部分数据,因为它太长了):

{
    "level_1_data": [
        [
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1,
            1
        ],
        [
            1,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            0,
            1
        ]
}

I am not sure if saving the file in your desired format is possible with json library.我不确定是否可以使用 json 库以所需的格式保存文件。 One dirty trick I used was to assume the json.dumps object as string and perform some str replacement to format the data as desired.我使用的一个肮脏的技巧是假设 json.dumps object 作为字符串并执行一些 str 替换以根据需要格式化数据。

Please try the code below:请尝试以下代码:

dict = data

def format_json(data):
    digits = [0,1,2,3,4,5,6,7,8,9]
    for i in digits:
        i = str(i)
        data = data.replace((i+',\n'), (i+',')) # remove \n after digit and comma
        data = data.replace(',            ', ',') # remove unwanted space
        data = data.replace(('\n            '+i), i) # remove \n and unwanted space before digit
        data = data.replace((i+'\n        '), i) #remove unwanted space and \n after digit
    data = data.replace(',', ', ') # add space after comma for readability
    return data

final = format_json(json.dumps(dict, indent=4))

with open('final.json', 'w') as f:
    f.write(final)

# checking if the file is parsable
with open('final.json', 'r') as f:
    data = json.load(f)
    print(data['level_1_data'])

The json file will be saved as desired and it is also loadable after saving. json 文件将根据需要保存,保存后也可加载。 Please note that the format_json is specific for the data in above requested format.请注意,format_json 特定于上述请求格式的数据。

Output file(final.json): Output 文件(final.json):

{
    "level_1_data": [
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
        [1, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 1],
        [1, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 2, 2, 1]
    ],
    "level_2_data": [
        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
        [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
        [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
        [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
        [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
    ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM