Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.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 如何将分号分隔的文件转换为嵌套的dict?_Python_Dictionary - Fatal编程技术网

Python 如何将分号分隔的文件转换为嵌套的dict?

Python 如何将分号分隔的文件转换为嵌套的dict?,python,dictionary,Python,Dictionary,我正在尝试将以分号分隔的文件转换为嵌套的dict。今天早上我一直在做这方面的工作,我想我忽略了一些简单的事情: 输入(样本) 这实际上大约有200行长。只是一个小样本 key;name;desc;category;type;action;range;duration;skill;strain_mod;apt_bonus ambiencesense;Ambience Sense;This sleight provides the async with an instinctive sense ab

我正在尝试将以分号分隔的文件转换为嵌套的dict。今天早上我一直在做这方面的工作,我想我忽略了一些简单的事情:

输入(样本) 这实际上大约有200行长。只是一个小样本

key;name;desc;category;type;action;range;duration;skill;strain_mod;apt_bonus
ambiencesense;Ambience Sense;This sleight provides the async with an instinctive sense about an area and any potential threats nearby. The async receives a +10 modifier to all Investigation, Perception, Scrounging, and Surprise Tests.;psi-chi;passive;automatic;self;constant;;0;
cogboost;Cognitive Boost;The async can temporarily elevate their cognitive performance.;psi-chi;active;quick;self;temp;;-1;{'COG': 5}
电流输出 期望输出 脚本(非功能)
这对于熊猫来说非常容易做到:

In [7]: import pandas as pd

In [8]: pd.read_clipboard(sep=";", index_col=0).T.to_dict()
Out[8]:
{'ambiencesense': {'action': 'automatic',
  'apt_bonus': nan,
  'category': 'psi-chi',
  'desc': 'This sleight provides the async with an instinctive sense about an area and any potential threats nearby. The async receives a +10 modifier to all Investigation, Perception, Scrounging, and Surprise Tests.',
  'duration': 'constant',
  'name': 'Ambience Sense',
  'range': 'self',
  'skill': nan,
  'strain_mod': 0,
  'type': 'passive'},
 'cogboost': {'action': 'quick',
  'apt_bonus': "{'COG': 5}",
  'category': 'psi-chi',
  'desc': 'The async can temporarily elevate their cognitive performance.',
  'duration': 'temp',
  'name': 'Cognitive Boost',
  'range': 'self',
  'skill': nan,
  'strain_mod': -1,
  'type': 'active'}}

在您的情况下,您会使用
pd.read\u csv()
而不是
.read\u clipboard()
,但看起来大致相同。如果要将
apt\u bonus
列解析为字典,您可能还需要对其进行一些调整。

这对于
pandas
来说非常容易:

In [7]: import pandas as pd

In [8]: pd.read_clipboard(sep=";", index_col=0).T.to_dict()
Out[8]:
{'ambiencesense': {'action': 'automatic',
  'apt_bonus': nan,
  'category': 'psi-chi',
  'desc': 'This sleight provides the async with an instinctive sense about an area and any potential threats nearby. The async receives a +10 modifier to all Investigation, Perception, Scrounging, and Surprise Tests.',
  'duration': 'constant',
  'name': 'Ambience Sense',
  'range': 'self',
  'skill': nan,
  'strain_mod': 0,
  'type': 'passive'},
 'cogboost': {'action': 'quick',
  'apt_bonus': "{'COG': 5}",
  'category': 'psi-chi',
  'desc': 'The async can temporarily elevate their cognitive performance.',
  'duration': 'temp',
  'name': 'Cognitive Boost',
  'range': 'self',
  'skill': nan,
  'strain_mod': -1,
  'type': 'active'}}

在您的情况下,您会使用
pd.read\u csv()
而不是
.read\u clipboard()
,但看起来大致相同。如果要将
apt\u bonus
列解析为字典,可能还需要对其进行一些调整。

尝试这种不使用库的python方式:

s = '''key;name;desc;category;type;action;range;duration;skill;strain_mod;apt_bonus
ambiencesense;Ambience Sense;This sleight provides the async with an instinctive sense about an area and any potential threats nearby. The async receives a +10 modifier to all Investigation, Perception, Scrounging, and Surprise Tests.;psi-chi;passive;automatic;self;constant;;0;
cogboost;Cognitive Boost;The async can temporarily elevate their cognitive performance.;psi-chi;active;quick;self;temp;;-1;{'COG': 5}'''

lists = [delim.split(';') for delim in s.split('\n')]
keyIndex = lists[0].index('key')
nested = {lst[keyIndex]:{lists[0][i]:lst[i] for i in range(len(lists[0])) if i != keyIndex} for lst in lists[1:]}
结果是:

{
    'cogboost': {
        'category': 'psi-chi',
        'name': 'Cognitive Boost',
        'strain_mod': '-1',
        'duration': 'temp',
        'range': 'self',
        'apt_bonus': "{'COG': 5}",
        'action': 'quick',
        'skill': '',
        'type': 'active',
        'desc': 'The async can temporarily elevate their cognitive performance.'
    },
    'ambiencesense': {
        'category': 'psi-chi',
        'name': 'Ambience Sense',
        'strain_mod': '0',
        'duration': 'constant',
        'range': 'self',
        'apt_bonus': '',
        'action': 'automatic',
        'skill': '',
        'type': 'passive',
        'desc': 'This sleight provides the async with an instinctive sense about an area and any potential threats nearby. The async receives a +10 modifier to all Investigation, Perception, Scrounging, and Surprise Tests.'
    }
}

尝试这种不使用库的Python方式:

s = '''key;name;desc;category;type;action;range;duration;skill;strain_mod;apt_bonus
ambiencesense;Ambience Sense;This sleight provides the async with an instinctive sense about an area and any potential threats nearby. The async receives a +10 modifier to all Investigation, Perception, Scrounging, and Surprise Tests.;psi-chi;passive;automatic;self;constant;;0;
cogboost;Cognitive Boost;The async can temporarily elevate their cognitive performance.;psi-chi;active;quick;self;temp;;-1;{'COG': 5}'''

lists = [delim.split(';') for delim in s.split('\n')]
keyIndex = lists[0].index('key')
nested = {lst[keyIndex]:{lists[0][i]:lst[i] for i in range(len(lists[0])) if i != keyIndex} for lst in lists[1:]}
结果是:

{
    'cogboost': {
        'category': 'psi-chi',
        'name': 'Cognitive Boost',
        'strain_mod': '-1',
        'duration': 'temp',
        'range': 'self',
        'apt_bonus': "{'COG': 5}",
        'action': 'quick',
        'skill': '',
        'type': 'active',
        'desc': 'The async can temporarily elevate their cognitive performance.'
    },
    'ambiencesense': {
        'category': 'psi-chi',
        'name': 'Ambience Sense',
        'strain_mod': '0',
        'duration': 'constant',
        'range': 'self',
        'apt_bonus': '',
        'action': 'automatic',
        'skill': '',
        'type': 'passive',
        'desc': 'This sleight provides the async with an instinctive sense about an area and any potential threats nearby. The async receives a +10 modifier to all Investigation, Perception, Scrounging, and Surprise Tests.'
    }
}

这是另一个版本,有点长,但没有依赖性

file_name = "<path>/test.txt"

data = {}
with open(file_name, 'r') as test_fh:
    header_line = next(test_fh)
    header_line = header_line.strip()
    headers = header_line.split(';')

    index_headers = {index:header for index, header in enumerate(headers)}

    for line in test_fh:
        line = line.strip()
        values = line.split(';')
        index_vals = {index:val for index, val in enumerate(values)}
        data[index_vals[0]] = {index_headers[key]:value for key, value in index_vals.items() if key != 0}

print(data)
file_name=“/test.txt”
数据={}
打开(文件名为“r”)作为测试:
标题线=下一个(测试线)
收割台_线=收割台_线.strip()
页眉=页眉_行。拆分(“;”)
index_headers={index:index的头,枚举中的头(头)}
对于测试中的线路\u fh:
line=line.strip()
值=行。拆分(“;”)
index_vals={index:val表示索引,val表示枚举(值)}
data[index_vals[0]={index_headers[key]:key的值,index_vals.items()中的值,如果key!=0}
打印(数据)

这是另一个版本,有点冗长,但没有依赖性

file_name = "<path>/test.txt"

data = {}
with open(file_name, 'r') as test_fh:
    header_line = next(test_fh)
    header_line = header_line.strip()
    headers = header_line.split(';')

    index_headers = {index:header for index, header in enumerate(headers)}

    for line in test_fh:
        line = line.strip()
        values = line.split(';')
        index_vals = {index:val for index, val in enumerate(values)}
        data[index_vals[0]] = {index_headers[key]:value for key, value in index_vals.items() if key != 0}

print(data)
file_name=“/test.txt”
数据={}
打开(文件名为“r”)作为测试:
标题线=下一个(测试线)
收割台_线=收割台_线.strip()
页眉=页眉_行。拆分(“;”)
index_headers={index:index的头,枚举中的头(头)}
对于测试中的线路\u fh:
line=line.strip()
值=行。拆分(“;”)
index_vals={index:val表示索引,val表示枚举(值)}
data[index_vals[0]={index_headers[key]:key的值,index_vals.items()中的值,如果key!=0}
打印(数据)

您始终可以使用
csv
模块。该类接受一个
分隔符
kwarg,您可以将其设置为“;”。然后,转换
DictReader
生成的字典是一项简单的任务。您返回dict\u list,因此它下面的所有代码都不会运行,您也不会生成字典。这并不能解决所有问题,但您可能可以从中找到答案。您可以始终使用
csv
模块。该类接受一个
分隔符
kwarg,您可以将其设置为“;”。然后,转换
DictReader
生成的字典是一项简单的任务。您返回dict\u list,因此它下面的所有代码都不会运行,您也不会生成字典。这并不能解决所有问题,但你可能可以从中找到答案。这最终奏效了。有点兴奋。把我的完整工作脚本放在上面。这最终成功了。有点兴奋。把我的完整工作脚本放在上面。我喜欢这个简短的解决方案,但我发现更详细的选项更容易理解。谢谢你的提示!我喜欢这个简短的解决方案,但我发现更详细的选项更容易理解。谢谢你的提示!感谢您的回答,但我不想引入新的依赖项。在对熊猫进行更深入的研究之后,它似乎对我遇到的一些不同的问题非常有用,因此我可能会重新思考它。感谢答案,但我不想引入新的依赖关系。在对熊猫进行更深入的研究之后,它肯定会对我遇到的一些不同的问题非常有用,所以我可能会重新思考它。
file_name = "<path>/test.txt"

data = {}
with open(file_name, 'r') as test_fh:
    header_line = next(test_fh)
    header_line = header_line.strip()
    headers = header_line.split(';')

    index_headers = {index:header for index, header in enumerate(headers)}

    for line in test_fh:
        line = line.strip()
        values = line.split(';')
        index_vals = {index:val for index, val in enumerate(values)}
        data[index_vals[0]] = {index_headers[key]:value for key, value in index_vals.items() if key != 0}

print(data)