如何将txt文件数据解析为结构化JSON-fi

2024-04-27 05:20:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个大的文本文件,其结构如下:

    2018-12-02

    Blue: 25 lux
    Green: 7 lux
    Red: 16 lux
    Blue: 25 lux
    Green: 7 lux
    Red: 16 lux
    Blue: 25 lux
    Green: 7 lux
    Red: 16 lux
    Blue: 24 lux

    .....

    avgBlue: 29.80 lux
    avgGreen: 8.40 lux
    avgRed: 19.40 lux

当我试图为json文件获取这种格式时,我无法正确解析该文件

^{pr2}$

但我不知道我会怎么做剧本。如果我不熟悉python,任何帮助都将不胜感激。在


Tags: 文件json格式greenbluered结构文本文件
2条回答

首先,您需要将文本文件转换为字典。使用json库将dict转换为json文件非常简单,但在此之前,我们必须读入该文件。在

在这里,似乎最简单的方法是使用一个有限状态机。基本上,一次读一行,根据我们刚刚读到的内容,根据需要迭代地添加到dict中。在

my_file = open('my_file.txt', 'r')
state = 0                           # Initialize our program's state
my_dict = {"Date": {}}              # Initialize the dict we're putting everything in
for line in my_file:
    if len(line.strip()) == 0:
        continue                    # skip blank lines
    if state == 0:
        # we're expecting a date on this line
        current_date = line.strip()
        my_dict["Date"][current_date] = []  # Initialize this date in the dict
        state = 1
    elif state == 1:
        # we're expecting a Blue or an avgBlue
        if line.find('avg') == 0:
            # we found 'avg', so we're looking for avgBlue
            value = line.split(':')[-1].strip()  # get the string after the ':'
            my_dict["Date"][current_date].append({"Averages":{"avgBlue": value}})
            state = 4
        elif line.find('Blue') == 0:
            # we found 'Blue'
            value = line.split(':')[-1].strip() 
            my_dict["Date"][current_date].append({"Blue": value})
            state = 2
        else:
            # we start a new date or something
            ...
    elif state == 2:
        # we're expecting a Green
        value = line.split(':')[-1].strip()
        my_dict["Date"][current_date][-1]["Green"] = value
        state = 3
    elif state == 3:
        # we're expecting a Red
        value = line.split(':')[-1].strip()
        my_dict["Date"][current_date][-1]["Red"] = value
        state = 1
    elif state == 4:
        ...

my_file.close()

老实说,这是一个相当复杂的构造,但是由于您的输入文件不是一种易于解析的格式,所以您可能会或多或少地受到它的限制。我不打算实现整个过程,您可能需要重写其中的大部分来处理特定的输入文件,但它应该作为一个起点。就其价值而言,有限状态机是计算机科学中更基本的原理之一,因此值得学习。在

一旦在字典中实际获得输入,将其输出为json非常简单:

^{pr2}$

我们需要更多关于如何对数据进行排序/组织的信息,但是如果要将数据读入python并将其保存为.json,则应该如下所示:

import json

input_data = []
with open('MyFile.txt', 'r') as file: # rename to your file
    for line in file.readlines():
        input_data.append(line.strip())

output_data = {}

# Your code to somehow sort input_data -> output_data

with open("NewJsonFile.json", "w+") as file: 
    json.dump(file, output_data)

我想您的排序算法将.split(':')数据(这将把一个字符串分隔成一个列表中的所需字符)。例如

^{pr2}$

['Green', ' 25 lux']

注意25勒克斯之前的空间!但是现在我们可以说

{"Green": " 25 lux"} 

等等等等!(您可能想.strip()从'25lux'的空间)

相关问题 更多 >