Python3-嵌套字典到JSON

时间:2019-05-13 18:25:12

标签: python python-3.x

我正在尝试将多个.txt文件转换为“表状”数据(具有列和行)。每个.txt文件都应视为一个新列。

请考虑以下.txt文件的内容:

File1.txt

Hi there
How are you doing?

What is your name?

File2.txt

Hi
Great!

Oliver, what's yours?

我创建了一个简单的方法,该方法可以接受文件和整数(另一个方法的文件编号):

def txtFileToJson(text_file, column):

    data = defaultdict(list)

    i = int(1)
    with open(text_file) as f:

        data[column].append(column)

        for line in f:
            i = i + 1
            for line in re.split(r'[\n\r]+', line):
                data[column] = line

    with open("output.txt", 'a+') as f:
        f.write(json.dumps(data))

因此上述方法将运行两次(每个文件一次,并附加数据)。

这是我运行脚本后的output.txt文件:

{"1": "What is your name?"}{"2": "Oliver, what's yours?"}

如您所见,我只能为每个文件创建一个新文件,然后添加整行。

[{
   "1": [{
       "1": "Hi there",
       "2": "How are you doing?",
       "3": "\n"
       "4": "What is your name?"
   },
   "2": [{
       "1": "Hi"
       "2": "Great!",
       "3": "\n",
       "4": "Oliver, what's yours?"
   },
}]

更新:

好的,所以我玩了一会儿,然后走近一点:

myDict = {str(column): []}
i = int(1)
with open(text_file) as f:
    for line in f:
        # data[column].append(column)
        match = re.split(r'[\n\r]+', line)
        if match:
            myDict[str(column)].append({str(i): line})
            i = i + 1

with open(out_file, 'a+') as f:
    f.write(json.dumps(myDict[str(column)]))

这给了我下面的输出:

[{"1": "Hi there\n"}, {"2": "How are you doing?\n"}, {"3": "\n"}, {"4": "What is your name?"}]
[{"1": "Hi\n"}, {"2": "Great!\n"}, {"3": "\n"}, {"4": "Oliver, what's yours?"}]

但是您可以看到,现在我有多个JSON根元素。

解决方案

多亏了炸薯条,我做到了:

 data = defaultdict(list)

 for path in images.values():
     column = column + 1

     data[str(column)] = txtFileToJson(path, column)

 saveJsonFile(path, data)

然后添加了一个新方法来保存最终的组合列表:

def saveJsonFile(text_file, data):

    basename = os.path.splitext(os.path.basename(text_file))
    dir_name = os.path.dirname(text_file) + "/"
    text_file = dir_name + basename[0] + "1.txt"

    out_file = dir_name + 'table_data.txt'

    with open(out_file, 'a+') as f:
        f.write(json.dumps(data))

3 个答案:

答案 0 :(得分:1)

您正在函数内部创建一个新字典。因此,每次在其中传递文本文件时,都会创建一个新的字典。

最简单的解决方案似乎是返回创建的字典并将其添加到现有字典中。

def txtFileToJson(text_file, column):
    myDict = {str(column): []}
    i = int(1)
    with open(text_file) as f:
        for line in f:
            # data[column].append(column)
            match = re.split(r'[\n\r]+', line)
            if match:
                myDict[str(column)].append({str(i): line})
                i = i + 1

    with open(out_file, 'a+') as f:
        f.write(json.dumps(myDict[str(column)]))

    return myDict

data = defaultdict(list)

data["1"] = txtFileToJson(text_file, column)
data["2"] = txtFileToJson(other_text_file, other_column)

答案 1 :(得分:0)

/home/springboot-app/properties/application-dev.yml,/home/springboot-app/properties/application-sqs.yml,/home/springboot-app/properties/application-redis.yml

答案 2 :(得分:0)

首先,如果我理解您正在尝试将字典词典作为输出,那么让我观察一下,我理解为您想要的输出似乎正在将整个内容包含在列表中,此外,您的打开不平衡和字典中的封闭列表括号,我将忽略它们,就像封闭列表一样。

我认为您需要类似的东西:

#!python3

import json
import re

def processTxtFile(text_file, n, data):
    d = {}
    with open(text_file) as f:
        i = 0
        for line in f:
            for line in re.split(r'[\n\r]+', line):
                i = i + 1
                d[str(i)] = line
    data[str(n)] = d


data = dict()
processTxtFile('File1.txt', 1, data)
processTxtFile('File2.txt', 2, data)
with open("output.txt", 'wt') as f:
    f.write(json.dumps(data))

如果您确实需要将嵌套词典包含在列表中,请替换

data[str(n)] = d

具有:

data[str(n)] = [d]