Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Pandas提取配置文件(看起来像K/V,但不是)_Python_Pandas_Config - Fatal编程技术网

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

Python 使用Pandas提取配置文件(看起来像K/V,但不是),python,pandas,config,Python,Pandas,Config,我有一个以下格式的配置文件: Models{ Model1{ Description = "xxxx" Feature = "yyyy" EventType = [ "Type1", "Type2"] } Model2{ Description = "aaaa" Feature = "bbbb" EventType = [

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

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 |

首先,您应该将其转换为标准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

您提供的数据是否来自.ini文件?我认为您可能需要使用一些正则表达式将此字符串转换为类似于json的内容,然后使用
json.load(string)
将其转换为
pandas.io.json.json\u normalize
pandas.read\u json
数据扩展名为.config。但是格式看起来不像普通的配置文件。如果存在不一致的情况,有没有办法找到“模型”?例如,abc模型、defg模型等等。当然有!我鼓励您尝试创建自己的正则表达式,应用于您的dataNoted。感谢您的指导。