2017-09-05 3 views
0

私は解析し、モデル(ユーザ)にデータを入れたいと思います。私はエラーを得た 、TypeError: 'Cell'オブジェクトはインデックス作成をサポートしていません

Traceback (most recent call last): 
    File "<console>", line 1, in <module> 
    File "/Users/xxx/testapp/app/views.py", line 17, in <module> 
    is_man = row[4] != "" 
TypeError: 'Cell' object does not support indexing 

views.pyは

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

    book = xlrd.open_workbook('../data/data.xlsx') 
    sheet = book.sheet_by_index(1) 

    for row_index in range(sheet.nrows): 
     rows = sheet.row(row_index) 
     print(row[1]) 
    for row in rows: 
     is_man = row[4] != "" 
     user = User(user_id=row[1], name_id=row[2], age=row[3], man=is_man) 
     user.save() 

は、私は、行、セルが、リストされていないと思うので、私は実際にそれが「セル」と呼ばれる理由を理解することはできませんし、私はどのように修正する必要がありますこの。 私はこの問題を解決しようとしたので、

for row_index in range(sheet.nrows): 
     rows = sheet.row(row_index) 
     rows = list(rows) 
     print(rows) 

のようなリストに行のセルを変更しかし、同じエラーhappens.Whatが、私はこの問題を解決するために行う必要がありますか? は今、views.pyは、私はそれが私が唯一data.Itが私の理想的なデータではありませんuser_idを得ることができます

user_id 
1 
2 
3 

あるので、私は、各ユーザを取得したい、print(rows[1])内のデータを見た

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

    book = xlrd.open_workbook('../data/data.xlsx') 
    sheet = book.sheet_by_index(1) 

    for row_index in range(sheet.nrows): 
     rows = sheet.row(row_index) 
     print(rows[1]) 
    for row in rows: 
     is_man = rows[4] != "" 
     user = User(user_id=row[1], name_id=row[2], age=row[3], man=is_man) 
     user.save() 

です私はこの問題を解決し、print(rows[1][0])を書き換えしようとした

1 1 40  leader 

、などのデータので、私は私の理想的なことを行う必要がありますhappens.How TypeError: 'Cell' object does not support indexingエラー?

私はprint(rows[4])をoutputedするとExcelのデータが excel

+0

'sheet.row(row_index)'を 'rows'に追加しないでください。 – Bijoy

+0

@Bijoy私はurの意味をキャッチすることはできません。 – user8504021

+0

@ MD.KhairulBasarエラーが削除されました!! thx !!この部分では、user = user(user_id = row [1]、name_id = row [2]、age = row [3]、man = is_man)行を行に変更する必要がありますか?(行を使用すべきではありませんか?) – user8504021

答えて

1

利用sheet.row_values(row_index, start_col_index, end_col_index[option])の代わりsheet.row()あるのでsheet.rowは()CellオブジェクトではありませんCell値を返すので、それは

text:'man' 
empty:'' 
text:'●' 
empty:'' 

のようにそれは自然です。

私はこれがあなたに役立つことを願っています。

関連する問題