从 Python 中的 Excel 单元格创建字典
Creating a dictionary from Excel cell in Python
我有一个 Excel 文件,其中每个单元格都有一个标题及其值(整数、字符串或无),由 :
符号分隔。我想制作一本字典,其中一个键可以是一个标题。
excel 文件中的列如下所示:
Procedure: PicProc #cell 1
Mod: avB #cell 2
ImageFile: av.jpg #so on
Odor: 1
OlfClass: 1
Jitter: 9500
ScaleDur: 4500
OdorList.Cycle: 1
OdorList.Sample: 10
Running: OdorList
FixationBlack.OnsetDelay: 2893
FixationBlack.OnsetTime: 217369
FixationBlack.RESP:
FixationBlack1.OnsetDelay: 27
FixationBlack1.OnsetTime: 226896
FixationBlack1.RESP:
FixationYellow.RESP:
PicPresent.OnsetDelay: 34
PicPresent.OnsetTime: 227547
PicPresent.RESP:
RatingOnset: 230558
我想要这样的东西:
{'Procedure': 'PicProc','Mod': 'avB', 'ImageFile': 'av.jpg','Odor': '1'... }
我应该怎么做?
您可以使用此代码访问 excel 并获取值
正如您在图像中看到的,您可以获得第一行标题
所以你将有:区域; Sotto-area1 等等
并获取值的第二行、第三行等。
结果你会有
面积="Campalto"; Sotto-area1="" 等
# Reading an excel file using Python
import xlrd
# Give the location of the file
loc = ("path of file")
# To open Workbook
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
# For row 0 and column 0
sheet.cell_value(0, 0) //this will return the value of the first cell ... that has position 0,0
my_dict = {}
loc = ("path of file")
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
for i in range(1, sheet.nrows):
row = sheet.row_values(i)
my_dict[row[0]] = row[1]
print(my_dict)
希望对您有所帮助。
假设您的数据放在工作簿的第一个 sheet 中,在 column A
中您可以像这样提取数据:
import xlrd
wb = xlrd.open_workbook("path/to/file")
sheet = wb.sheet_by_index(0) # First sheet of workbook
arr = sheet.col_values(0) # Column A
print({k:v.strip() for k, v in dict(s.split(':', 1) for s in arr).items()})
输出
{'Procedure': 'PicProc', 'Mod': 'avB', 'ImageFile': 'av.jpg', 'Odor': '1', 'OlfClass': '1', 'Jitter': '9500', 'ScaleDur': '4500', 'OdorList.Cycle': '1', 'OdorList.Sample': '10', 'Running': 'OdorList', 'FixationBlack.OnsetDelay': '2893', 'FixationBlack.OnsetTime': '217369', 'FixationBlack.RESP': '', 'FixationBlack1.OnsetDelay': '27', 'FixationBlack1.OnsetTime': '226896', 'FixationBlack1.RESP': '', 'FixationYellow.RESP': '', 'PicPresent.OnsetDelay': '34', 'PicPresent.OnsetTime': '227547', 'PicPresent.RESP': '', 'RatingOnset': '230558'}
我有一个 Excel 文件,其中每个单元格都有一个标题及其值(整数、字符串或无),由 :
符号分隔。我想制作一本字典,其中一个键可以是一个标题。
excel 文件中的列如下所示:
Procedure: PicProc #cell 1
Mod: avB #cell 2
ImageFile: av.jpg #so on
Odor: 1
OlfClass: 1
Jitter: 9500
ScaleDur: 4500
OdorList.Cycle: 1
OdorList.Sample: 10
Running: OdorList
FixationBlack.OnsetDelay: 2893
FixationBlack.OnsetTime: 217369
FixationBlack.RESP:
FixationBlack1.OnsetDelay: 27
FixationBlack1.OnsetTime: 226896
FixationBlack1.RESP:
FixationYellow.RESP:
PicPresent.OnsetDelay: 34
PicPresent.OnsetTime: 227547
PicPresent.RESP:
RatingOnset: 230558
我想要这样的东西:
{'Procedure': 'PicProc','Mod': 'avB', 'ImageFile': 'av.jpg','Odor': '1'... }
我应该怎么做?
您可以使用此代码访问 excel 并获取值 正如您在图像中看到的,您可以获得第一行标题 所以你将有:区域; Sotto-area1 等等 并获取值的第二行、第三行等。 结果你会有 面积="Campalto"; Sotto-area1="" 等
# Reading an excel file using Python
import xlrd
# Give the location of the file
loc = ("path of file")
# To open Workbook
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
# For row 0 and column 0
sheet.cell_value(0, 0) //this will return the value of the first cell ... that has position 0,0
my_dict = {}
loc = ("path of file")
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
for i in range(1, sheet.nrows):
row = sheet.row_values(i)
my_dict[row[0]] = row[1]
print(my_dict)
希望对您有所帮助。
假设您的数据放在工作簿的第一个 sheet 中,在 column A
中您可以像这样提取数据:
import xlrd
wb = xlrd.open_workbook("path/to/file")
sheet = wb.sheet_by_index(0) # First sheet of workbook
arr = sheet.col_values(0) # Column A
print({k:v.strip() for k, v in dict(s.split(':', 1) for s in arr).items()})
输出
{'Procedure': 'PicProc', 'Mod': 'avB', 'ImageFile': 'av.jpg', 'Odor': '1', 'OlfClass': '1', 'Jitter': '9500', 'ScaleDur': '4500', 'OdorList.Cycle': '1', 'OdorList.Sample': '10', 'Running': 'OdorList', 'FixationBlack.OnsetDelay': '2893', 'FixationBlack.OnsetTime': '217369', 'FixationBlack.RESP': '', 'FixationBlack1.OnsetDelay': '27', 'FixationBlack1.OnsetTime': '226896', 'FixationBlack1.RESP': '', 'FixationYellow.RESP': '', 'PicPresent.OnsetDelay': '34', 'PicPresent.OnsetTime': '227547', 'PicPresent.RESP': '', 'RatingOnset': '230558'}