简体   繁体   English

将txt文件转换为python中的JSON

[英]Convert txt file into JSON in python

I have a txt file which have the format shown below and the Key strings are not in quotes. 我有一个txt文件,其格式如下所示,并且密钥字符串不在引号中。 How can I convert into a JSON using python? 如何使用python转换为JSON?

name {
  first_name: "random"
}
addresses {
  location {
    locality: "India"
    street_address: "xyz"
    postal_code: "300092"
    full_address: "street 1 , abc,India"
  }
}
projects {
  url: "www.githib.com"
}

Assuming your data as, 假设您的数据为

{
    'addresses': {
        'location': {
            'full_address': 'street 1 , abc,India',
            'locality': 'India',
            'postal_code': '300092',
            'street_address': 'xyz'
        }
    },
    'name': {
        'first_name': 'random'
    },
    'projects': {
        'url': 'www.githib.com'
    }
}

Use json.dumps to convert dict to json 使用json.dumps将dict转换为json

In [16]: import json

In [17]: data
Out[17]:
{'addresses': {'location': {'full_address': 'street 1 , abc,India',
   'locality': 'India',
   'postal_code': '300092',
   'street_address': 'xyz'}},
 'name': {'first_name': 'random'},
 'projects': {'url': 'www.githib.com'}}

In [18]: json.dumps(data)
Out[18]: '{"name": {"first_name": "random"}, "projects": {"url": "www.githib.com"}, "addresses": {"location": {"postal_code": "300092", "full_address": "street 1 , abc,India", "street_address": "xyz", "locality": "India"}}}'

In [19]:

There's no simple way in the standard library to convert that data format to JSON, so we need to write a parser. 标准库中没有简单的方法可以将数据格式转换为JSON,因此我们需要编写一个解析器。 However, since the data format is fairly simple that's not hard to do. 但是,由于数据格式相当简单,因此并不难。 We can use the standard csv module to read the data. 我们可以使用标准的csv模块读取数据。 The csv.reader will handle the details of parsing spaces and quoted strings correctly. csv.reader将正确处理解析空格和带引号的字符串的详细信息。 A quoted string will be treated as a single token, tokens consisting of a single word may be quoted but they don't need to be. 带引号的字符串将被视为单个标记,可以包含由单个单词组成的标记,但不一定需要。

The csv.reader normally gets its data from an open file, but it's quite versatile, and will also read its data from a list of strings. csv.reader通常从打开的文件中获取其数据,但是它用途广泛,并且还将从字符串列表中读取其数据。 This is convenient while testing since we can embed our input data into the script. 在测试时这很方便,因为我们可以将输入数据嵌入脚本中。

We parse the data into a nested dictionary. 我们将数据解析为嵌套字典。 A simple way to keep track of the nesting is to use a stack, and we can use a plain list as our stack. 跟踪嵌套的一种简单方法是使用堆栈,我们可以使用普通列表作为堆栈。

The code below assumes that input lines can be one of three forms: 下面的代码假定输入行可以是以下三种形式之一:

  1. Plain data. 普通数据。 The line consists of a key - value pair, separated by at least one space. 该行由键-值对组成,并由至少一个空格分隔。
  2. A new subobject. 一个新的子对象。 The line starts with a key and ends in an open brace { . 该行以一个键开头,并以一个大括号{结束。
  3. The end of the current subobject. 当前子对象的末尾。 The line contains a single close brace } 该行包含一个右大括号}

import csv
import json

raw = '''\
name {
  first_name: "random"
}
addresses {
  location {
    locality: "India"
    street_address: "xyz"
    postal_code: "300092"
    full_address: "street 1 , abc,India"
  }
}
projects {
  url: "www.githib.com"
}
'''.splitlines()

# A stack to hold the parsed objects
stack = [{}]

reader = csv.reader(raw, delimiter=' ', skipinitialspace=True)
for row in reader:
    #print(row)
    key = row[0]
    if key == '}':
        # The end of the current object
        stack.pop()
        continue
    val = row[-1]
    if val == '{':
        # A new subobject
        stack[-1][key] = d = {}
        stack.append(d)
    else:
        # A line of plain data
        stack[-1][key] = val

# Convert to JSON
out = json.dumps(stack[0], indent=4)
print(out)

output 输出

{
    "name": {
        "first_name:": "random"
    },
    "addresses": {
        "location": {
            "locality:": "India",
            "street_address:": "xyz",
            "postal_code:": "300092",
            "full_address:": "street 1 , abc,India"
        }
    },
    "projects": {
        "url:": "www.githib.com"
    }
}

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

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