Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.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
excel中的Python字典_Python_Excel_Dictionary - Fatal编程技术网

excel中的Python字典

excel中的Python字典,python,excel,dictionary,Python,Excel,Dictionary,我需要从excel文件创建字典。这看起来像这样: Key Value aa A ab A ac A ad B ae B af B ag B ah B 结果将是一本字典,看起来像: dictionary ={'aa': 'A', 'ab': 'A', 'ac': 'A', 'ad': 'B', 'ae': 'B', 'af': 'B', 'ag': 'B', 'ah': 'B' } 快速示例: import csv dictionary

我需要从excel文件创建字典。这看起来像这样:

Key   Value
aa    A
ab    A
ac    A
ad    B
ae    B
af    B
ag    B
ah    B
结果将是一本字典,看起来像:

dictionary ={'aa': 'A', 'ab': 'A', 'ac': 'A', 'ad': 'B', 'ae': 'B', 'af': 'B', 'ag': 'B', 'ah': 'B' } 
快速示例:

import csv

dictionary = {}
with open('data.csv', 'r') as f:
    reader = csv.DictReader(f)
    for row in reader:
        dictionary[row['Key']] = row['Value']

看起来确实是这样!请看这里:0另存为csv并将csv模块与csv.dictReader一起使用您的示例的输出是{'ad':'B','ag':'B','ae':'B','ab':'a','af':'B','ah':'B','ac':'a','aa':'a'}所以您很好。thx,我只是快速地把它组合起来。。。每当我看到一个excel问题,它通常可以通过stdlib csv模块解决。很高兴知道。我还没有用excel做过很多工作,但我相信我将来会这样做,所以这会很有用。