将文本文件内容输出到JSON Python

yws3nbqq  于 2022-12-15  发布在  Python
关注(0)|答案(3)|浏览(157)

我想使用Python将以下文本文件输出为JSON格式。

我不知道从哪里开始,但我希望我的问题是简单易懂的。
input.txt

Name 1, FeatureServer, thisIsALink, layer 1, layer 2
Name 2, FeatureServer, thisIsALink, layer 1
Name 3, MapServer, thisIsALink, layer 1, layer 2
Name 4, FeatureServer, thisIsALink, layer 1, layer 2
Name 5, FeatureServer, thisIsALink, layer 1, layer 2, layer 3

output.json

{
    "Name 1": {
        "type": "FeatureServer",
        "url": "thisIsALink",
        "layer(s)": {
            "1": "layer 1",
            "2": "layer 2"
        }
    },
    "Name 2": {
        "type": "FeatureServer",
        "url": "thisIsALink",
        "layer(s)": {
            "1": "layer 1"
        }
    },
    "Name 3": {
        "type": "FeatureServer",
        "url": "thisIsALink",
        "layer(s)": {
            "1": "layer 1",
            "2": "layer 2"
        }
    },
    "Name 4": {
        "type": "FeatureServer",
        "url": "thisIsALink",
        "layer(s)": {
            "1": "layer 1",
            "2": "layer 2"
        }
    },
    "Name 5": {
        "type": "FeatureServer",
        "url": "thisIsALink",
        "layer(s)": {
            "1": "layer 1",
            "2": "layer 2",
            "3": "layer 3"
        }
    }
}

我试过跟随GeeksforGeeks的教程
但我还没想好怎么让它适合我的情况。

e0bqpujr

e0bqpujr1#

下面是一个小脚本,它可以做到这一点:

import json

output = {}
with open("input.txt", "r") as f:
    for line in f:
        # Split the line, preserve all layers
        name, server, url, *layers = line.rstrip().split(", ")
        output[name] = {
            "type": server,
            "url": url,
            "layer(s)": {str(i): layer for i, layer in enumerate(layers, 1)}
        }

with open("output.json", "w") as f:
    json.dump(output, f, indent=4)
iaqfqrcu

iaqfqrcu2#

根据您提供的信息,以下是一种方法:

import json

def main():
    file = './74724498/input.txt'
    output_dict = {}
    with open(file) as f:
        for line in f:
            # Strip the newline character and split the line into a list
            line_ls = line.strip().split(", ")
            # Create a dictionary for each row:
            row_dict = {
                line_ls[0]: {
                    "type": line_ls[1],
                    "url": line_ls[2],
                    # Create a dictionary for each layer:
                    "layer(s)": {str(index): val for index, val in enumerate(line_ls[3:], start=1)}
                    }
                }
            # Update the output dictionary with the row dictionary:
            output_dict.update(row_dict)
        
        # Write the output dictionary to a json file, indenting the output for readability:
        json_string = json.dumps(output_dict, indent=4)
        # Write the json string to a file:
        with open('json_data.json', 'w') as outfile:
            outfile.write(json_string)    
        
if __name__ == '__main__':
    main()

玩一下,看看这是不是你要找的!

4ngedf3f

4ngedf3f3#

问题是你的“层”需要嵌套在json中,而没有办法用csv来表示,如果暗示第4列、第5列(以及更远的列)应该嵌套,你可以通过编程来解决。
我假设这是你所指的教程:
https://www.geeksforgeeks.org/convert-csv-to-json-using-python/

相关问题