Python 如何将excel数据放入字典?

Python 如何将excel数据放入字典?,python,django,excel,Python,Django,Excel,我想把excel数据放到字典里。 卓越是 views.py是 #coding:utf-8 from django.shortcuts import render import xlrd book3 = xlrd.open_workbook('./data/excel.xlsx') sheet3 = book3.sheet_by_index(0) large_item = None data_dict = {} for row_index in range(1,sheet3.nrows):

我想把excel数据放到字典里。 卓越是 views.py是

#coding:utf-8
from django.shortcuts import render
import xlrd

book3 = xlrd.open_workbook('./data/excel.xlsx')
sheet3 = book3.sheet_by_index(0)

large_item = None
data_dict = {}
for row_index in range(1,sheet3.nrows):
    rows3 = sheet3.row_values(row_index)
    large_item = rows3[1] or large_item
    data_dict = rows3
现在,当我打印出
print(data_dict)
时,屏幕上显示了['''4','10','Karen',''''.''。之前,我写了
data_dict.extend(rows3)
来代替
data_dict=rows3
,但那时dict没有发生扩展错误。我理想的输出是

data_dict = {
    1: {
        user_id: 1,
        name_id: 1,
        name: Blear,
        age: 40,
        man: false,
        employee: leader,
    },
    2: {
        user_id: 2,
        name_id: 5,
           ・
       ・
       ・
    },
           ・
       ・
       ・
}
我应该如何写作来实现我的目标?

你的问题是:

data_dict = rows3
这不会将行3添加到数据目录,此集合是值。所以data_dict等于最后一行

要将元素添加到dict,您需要执行以下操作:

data_dict[KEY] = VALUE
您的键将是行索引

现在,您需要另一个类似于dict的值

{
    user_id: 1,
    name_id: 1,
    name: Blear,
    age: 40,
    man: false,
    employee: leader,
}
因此,对于需要构造此dict的每一行,使用头和单元格值来执行此操作

#coding:utf-8
from django.shortcuts import render
import xlrd

book3 = xlrd.open_workbook('./data/excel.xlsx')
sheet3 = book3.sheet_by_index(0)

headers = sheet3.row_values(0)
large_item = None
data_dict = {}
for row_index in range(1,sheet3.nrows):
    rows3 = sheet3.row_values(row_index)
    large_item = rows3[1] or large_item

    # Create dict with headers and row values
    row_data = {}
    for idx_col,value in enumerate(rows3):
        header_value = headers[idx_col]
        # Avoid to add empty column. A column in your example
        if header_value:
            row_data[headers[idx_col]] = value
     # Add row_data to your data_dict with 
     data_dict[row_index] = row_data
我不测试这段代码,它只是给你一个如何做的想法

#coding:utf-8
from django.shortcuts import render
import xlrd

book3 = xlrd.open_workbook('./data/excel.xlsx')
sheet3 = book3.sheet_by_index(0)

headers = sheet3.row_values(0)
large_item = None
data_dict = {}
for row_index in range(1,sheet3.nrows):
    rows3 = sheet3.row_values(row_index)
    large_item = rows3[1] or large_item

    # Create dict with headers and row values
    row_data = {}
    for idx_col,value in enumerate(rows3):
        header_value = headers[idx_col]
        # Avoid to add empty column. A column in your example
        if header_value:
            row_data[headers[idx_col]] = value
     # Add row_data to your data_dict with 
     data_dict[row_index] = row_data
你的问题是:

data_dict = rows3
这不会将行3添加到数据目录,此集合是值。所以data_dict等于最后一行

要将元素添加到dict,您需要执行以下操作:

data_dict[KEY] = VALUE
您的键将是行索引

现在,您需要另一个类似于dict的值

{
    user_id: 1,
    name_id: 1,
    name: Blear,
    age: 40,
    man: false,
    employee: leader,
}
因此,对于需要构造此dict的每一行,使用头和单元格值来执行此操作

#coding:utf-8
from django.shortcuts import render
import xlrd

book3 = xlrd.open_workbook('./data/excel.xlsx')
sheet3 = book3.sheet_by_index(0)

headers = sheet3.row_values(0)
large_item = None
data_dict = {}
for row_index in range(1,sheet3.nrows):
    rows3 = sheet3.row_values(row_index)
    large_item = rows3[1] or large_item

    # Create dict with headers and row values
    row_data = {}
    for idx_col,value in enumerate(rows3):
        header_value = headers[idx_col]
        # Avoid to add empty column. A column in your example
        if header_value:
            row_data[headers[idx_col]] = value
     # Add row_data to your data_dict with 
     data_dict[row_index] = row_data
我不测试这段代码,它只是给你一个如何做的想法

#coding:utf-8
from django.shortcuts import render
import xlrd

book3 = xlrd.open_workbook('./data/excel.xlsx')
sheet3 = book3.sheet_by_index(0)

headers = sheet3.row_values(0)
large_item = None
data_dict = {}
for row_index in range(1,sheet3.nrows):
    rows3 = sheet3.row_values(row_index)
    large_item = rows3[1] or large_item

    # Create dict with headers and row values
    row_data = {}
    for idx_col,value in enumerate(rows3):
        header_value = headers[idx_col]
        # Avoid to add empty column. A column in your example
        if header_value:
            row_data[headers[idx_col]] = value
     # Add row_data to your data_dict with 
     data_dict[row_index] = row_data

您可以使用python的库
pandas
获得一个简单的解决方案:

from pandas import *
xls = ExcelFile('your_excel_file.xls')
df = xls.parse(xls.sheet_names[0])
df.to_dict()

您可以使用python的库
pandas
获得一个简单的解决方案:

from pandas import *
xls = ExcelFile('your_excel_file.xls')
df = xls.parse(xls.sheet_names[0])
df.to_dict()

我们可以解决这个问题并简化它。我们可以解析csv,而不是解析excel。有很多库支持解析CSV,所以我建议我们这样做。我们可以解决这个问题并简化它。我们可以解析csv,而不是解析excel。有很多库支持解析CSV,所以我建议我们这样做。