使用python,我正在使用以下示例数据从文本文件导入数据:
ABB : {'Code': 'adr', 'Volume': 2238117, 'Sector': 'Industrials', 'Market_Cap': 'No Data', 'Industry': 'Industrial Products', 'List_Date': '2001-04-06'},
ABEV : {'Code': 'adr', 'Volume': 19348239, 'Sector': 'Consumer Defensive', 'Market_Cap': 'No Data', 'Industry': 'Beverages - Alcoholic', 'List_Date': '2013-11-11'},
我将其导入到包含以下代码段的字典中:
with open('list_all.csv', mode='r') as infile:
reader = csv.reader(infile)
result = {}
for row in reader:
key = row[0]
result[key] = row[1:]
它确实被导入为字典,但问题是因为KEY不在""例如"ABB"
或"ABEV"
。一旦我导入它我的dic看起来像:
"ABB : {'Code': 'adr'": [" 'Volume': 2238117",
" 'Sector': 'Industrials'",
" 'Market_Cap': 'No Data'",
" 'Industry': 'Industrial Products'",
" 'List_Date': '2001-04-06'}",
''],
尝试解决此问题的最佳方法是什么
答案 0 :(得分:1)
通过它的外观,您可以逐行阅读,删除任何尾随的逗号并拆分dt.Setters.Add(new Setter(DataGridCell.MarginProperty, new Binding()
{
Path = new PropertyPath("StatusColumnMargin"),
Mode = BindingMode.OneWay,
Source = this
}));
和:
ast.literal_eval
部分,例如:
dict
给你import ast
with open('yourfile') as fin:
rows = (line.rstrip('\n,').partition(' : ') for line in fin)
data = {r[0]: ast.literal_eval(r[2]) for r in rows}
:
data