使用Pandas提取配置文件(看起来像K/V,但不是)

2024-04-26 13:57:28 发布

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

我有一个以下格式的配置文件:

Models{
    Model1{
        Description = "xxxx"
        Feature = "yyyy"
        EventType = [
            "Type1", 
            "Type2"]
    }

    Model2{
        Description = "aaaa"
        Feature = "bbbb"
        EventType = [
            "Type3", 
            "Type4"]
    }
}

有没有办法将其转换为如下所示的数据帧

|Model  | Description | Feature | EventType    | 
------------------------------------------------
|Model1 | xxxx        | yyyy    | Type1, Type2 |
|Model2 | aaaa        | bbbb    | Type3, Type4 |

Tags: 格式配置文件descriptionfeaturemodel1aaaaxxxxyyyy
1条回答
网友
1楼 · 发布于 2024-04-26 13:57:28

首先,您应该将其转换为标准JSON格式。您可以使用正则表达式来实现这一点:

with open('untitled.txt') as f:
    data = f.read()

import re
# Converting into JSON format
data = re.sub(r'(=\s*".*")\n', r'\1,\n', data)
data = re.sub(r'(Description|Feature|EventType)', r'"\1"', data)
data = re.sub(r'}(\s*Model[0-9]+)', r'},\1', data)
data = re.sub(r'(Model[0-9]+)', r'"\1"=', data)
data = re.sub(r'(Models)', r'', data)
data = re.sub(r'=', r':', data)

您的文件将如下所示:

{
    "Model1":{
        "Description" : "xxxx",
        "Feature" : "yyyy",
        "EventType" : [
            "Type1", 
            "Type2"]
    },

    "Model2":{
        "Description" : "aaaa",
        "Feature" : "bbbb",
        "EventType" : [
            "Type3", 
            "Type4"]
    }
}

然后,使用pd.read_json阅读:

import pandas as pd
from io import StringIO

df = pd.read_json(StringIO(data), orient='index').reset_index()
#        index Description       EventType Feature
#0  Model1        xxxx  [Type1, Type2]    yyyy
#1  Model2        aaaa  [Type3, Type4]    bbbb

相关问题 更多 >