質問編集履歴

1

情報の修正

2017/09/09 04:38

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,123 +1,3 @@
1
1
  TypeError: string indices must be integers とエラーが出ました。
2
2
 
3
- Traceback には
4
-
5
- ```ここに言語を入力
6
-
7
- Traceback (most recent call last):
8
-
9
- File "<console>", line 1, in <module>
10
-
11
- File "/Users/XXX/testapp/app/views.py", line 164, in <module>
12
-
13
- user1 = User.objects.filter(user_id=row_data['user_id']).exists()
14
-
15
- TypeError: string indices must be integers
16
-
17
- ```
18
-
19
- と出ました。コードは
20
-
21
- ```ここに言語を入力
22
-
23
- #coding:utf-8
24
-
25
- from django.shortcuts import render
26
-
27
- import xlrd
28
-
29
- from .models import User
30
-
31
-
32
-
33
- book = xlrd.open_workbook('../data/excel1.xlsx')
34
-
35
- sheet = book.sheet_by_index(1)
36
-
37
-
38
-
39
- def build_employee(employee):
40
-
41
- if employee == 'leader':
42
-
43
- return 'l'
44
-
45
- if employee == 'manager':
46
-
47
- return 'm'
48
-
49
- if employee == 'others':
50
-
51
- return 'o'
52
-
53
-
54
-
55
- for row_index in range(sheet.nrows):
56
-
57
- rows = sheet.row_values(row_index)
58
-
59
- is_man = rows[4] != ""
60
-
61
- emp = build_employee(rows[5])
62
-
63
- user = User(user_id=rows[1], name_id=rows[2], name=rows[3],
64
-
65
- age=rows[4],man=is_man,employee=emp)
66
-
67
- user.save()
68
-
69
-
70
-
71
- files = glob.glob('./user/*.xlsx')
72
-
73
-
74
-
75
- for x in files:
76
-
77
- if "$" not in x:
78
-
79
- book3 = xlrd.open_workbook(x)
80
-
81
- sheet3 = book3.sheet_by_index(0)
82
-
83
- cells = [
84
-
85
- ]
86
-
87
- data_dict = OrderedDict()
88
-
89
- for key, rowy, colx in cells:
90
-
91
- try:
92
-
93
- data_dict[key] = sheet3.cell_value(rowy, colx)
94
-
95
- except IndexError:
96
-
97
- data_dict[key] = None
98
-
99
-
100
-
101
- for row_number, row_data in data_dict.items():
102
-
103
- user1 = User.objects.filter(user_id=row_data['user_id']).exists()
104
-
105
- if user1:
106
-
107
- user1.__dict__.update(**data_dict)
108
-
109
- user1.save()
110
-
111
- ```
112
-
113
- のように書きました。data_dictは辞書型なのでitems()を使いfor文を回したのですが、それでもエラーが出る理由がわかりません。
114
-
115
- data_dict は
116
-
117
- ```ここに言語を入力
118
-
119
- OrderedDict([('user_id', '1'), ('name', 'Blear'), ('nationality', 'America'), ('domitory', 'A'), ('group', 1)], [('user_id', '1'), ('name', 'Blear'), ('nationality', 'America'), ('domitory', 'A'), ('group', 1)], [('user_id', '1'), ('name', 'Blear'), ('nationality', 'America'), ('domitory', 'A'), ('group', 1)])
120
-
121
- ```
122
-
123
- のようになっています。
3
+ どう直せば良いのでしょうか?