我有一个看起来像这样的表:
Header = Category | US | UK | CA
Row 1 = A | value1 | value1 | value2
Row 2 = B | value2 | value1 | value3
Row 3 = C | value1 | value3 | value1
“类别”列包含唯一值。其余的列包含可以或不能唯一的值。读取方法是:对于A类,美国商品具有此值。
我正在尝试创建一个字典,以便键是类别,值是一个以国家为键,值是值的字典。
Dict = {A : {US : value1, UK : value1, CA : value2}, B :
{US:value2, UK:value1, CA:value3}, C :
{US:value1,UK:value3,CA:value1}}
这是一个很长的列表,所以我需要通过迭代来创建它。我整天都坚持下去。我可以正确创建密钥,但可以正确使用“字典值”。
有一种简单的方法吗?
答案 0 :(得分:1)
假设您的表是一个数组数组:
table = [[ 'Category', 'US', 'UK', 'CA' ], [ 'A', 'value1', 'value1', 'value2'], [ 'B', 'value2', 'value1', 'value2']]
dict = {table[i][0] : {table[0][j]: table[i][j] for j in range(1,len(table[i]))} for i in range(1,len(table))}
print(dict)
给你:
{'A':{'US':'value1','UK':'value1','CA':'value2'},'B':{'US': 'value2','UK':'value1','CA':'value2'}}
答案 1 :(得分:1)
像这样的事情应该起作用并且很容易理解,基本上只需在" | "
上拆分即可:
import pprint
def main():
pp = pprint.PrettyPrinter(indent=2)
path = "table.txt"
res = {}
with open(path, "r") as f:
catagories = f.readline().strip().split(" | ")[-3:]
for line in f:
key_part, *values = line.strip().split(" | ")
key = key_part.split()[-1]
res[key] = {
catagories[i]: values[i]
for i in range(len(catagories))
}
pp.pprint(res)
if __name__ == "__main__":
main()
table.txt:
Header = Category | US | UK | CA
Row 1 = A | value1 | value1 | value2
Row 2 = B | value2 | value1 | value3
Row 3 = C | value1 | value3 | value1
输出:
{
'A': {'CA': 'value2', 'UK': 'value1', 'US': 'value1'},
'B': {'CA': 'value3', 'UK': 'value1', 'US': 'value2'},
'C': {'CA': 'value1', 'UK': 'value3', 'US': 'value1'}
}
答案 2 :(得分:0)
熊猫到字典Pandas to Dict,也许从文本文件加载到熊猫,然后转换为dict,将索引设置为Category。
例如:
import pandas as pd
df = pd.read_csv("data.csv", sep=",")
s = df.set_index('Category').T.to_dict('series')
print(s)
data.csv
Category,US,UK,CA
A,1,1,1
B,2,2,2
C,3,3,3
答案 3 :(得分:0)
正确地将值放入字典的方法是,只需将它们分配给键:
dictionary[key] = v
因为您希望将字典作为值,所以只需要用{US : value1, UK : value1, CA : value2}
来写v
或类似的内容来代替'value1, value2
',依此类推,以此类推。价值观。
如果您将US,UK和CA作为字符串,而不将内部内容作为自定义内容的变量作为内字典的键,则写"UK": value1
而不是UK: value1
答案 4 :(得分:0)
假设您的表与您的问题完全相同,并且位于文件sapehi.txt
中,这应该可以满足您的要求。
f = open("sapehi.txt", "r")
first = f.readline()
table = first.split("=")[1] # Throw away the "Header =" part
columns = [x.strip() for x in table.split("|")][1:] # Create a list of the column headings
output = {} # Create the output dictionary
while True:
line = f.readline()
if (line == ""): # If there's no more data in the file, exit the loop
break
row = line.split("=")[1] # Throw away the "Row x" part
values = [x.strip() for x in row.split("|")] # Create a row elements
category = values[0] # Category is the first row element
values = values[1:] # The values are all the rest
output[category] = {} # Create a dict for this category
for index, value in enumerate(values):
output[category][columns[index]] = value # Populate it with the values
print(output)
此脚本将列标题添加到列表中,每一行都可以访问该列表。