我直接把代码移进的 view,然后用的一个函数调用这个类 class TrainsCollection: header = '车次 车站 时间 历时 一等 二等 软卧 硬卧 硬座 无座'.split()
def __init__(self, available_trains, options):
"""
:param available_trains: 官网提取到的 json 数据列表
:param options: 查询的选项, 如高铁, 动车, etc...
"""
self.available_trains = available_trains
self.options = options
"""切割"""
def _cut_train(self):
saperated_info = []
for i in range(len(self.available_trains)):
saperated_info.append((self.available_trains[i]).split('|'))
"""构建字典列表"""
dict_header = ['train_number', 'from_station', 'to_station', 'from_time', 'to_time', 'spend_time',
'first_class', 'second_class', 'soft_sleeper', 'hard_sleeper', 'hard_seat', 'none_seat']
refer_to_num = [3, 4, 5, 8, 9, 10, 31, 30, 23, 26, 28, 29]
train_list = []
train_dict = {}
for times in range(len(saperated_info)):
for key in range(len(dict_header)):
if saperated_info[times][refer_to_num[key]]:
train_dict[dict_header[key]] = saperated_info[times][refer_to_num[key]]
else:
train_dict[dict_header[key]] = '--'
train_list.append(copy.deepcopy(train_dict))
return train_list
@property
def trains(self):
for raw_train in self._cut_train():
train_no = raw_train['train_number']
initial = train_no[0].lower()
if not self.options or initial in self.options:
train = [
train_no,
'\n'.join([Fore.GREEN + _get_keys(raw_train['from_station']) + Fore.RESET,
Fore.RED + _get_keys(raw_train['to_station']) + Fore.RESET]),
'\n'.join([Fore.GREEN + raw_train['from_time'] + Fore.RESET,
Fore.RED + raw_train['to_time'] + Fore.RESET]),
raw_train['spend_time'],
raw_train['first_class'],
raw_train['second_class'],
raw_train['soft_sleeper'],
raw_train['hard_sleeper'],
raw_train['hard_seat'],
raw_train['none_seat'],
]
yield train
def _get_keys(station_eg):
for key in stations:
if stations[key] == station_eg:
def pretty_print(self):
pt = PrettyTable()
pt._set_field_names(self.header)
for train in self.trains:
pt.add_row(train)
content = PrettyTable.whatever()
return HttpResponse('<pre>{}</pre>'.format(content))
错误提示: def pretty_print(self): ^ IndentationError: expected an indented block
1
lxy 2017-07-04 18:39:35 +08:00
错误提示很明白了,再认真看一下吧
|
2
arischow 2017-07-04 18:40:49 +08:00 via iPhone
错误提示写明白了……你需要一个 IDE
|
3
1002149271 OP @lxy 但是我这个缩进不是正常的吗
|
4
ysicing 2017-07-04 19:01:18 +08:00
```python
for key in stations: if stations[key] == station_eg: pass def pretty_print(self): pt = PrettyTable() ``` |
5
cszhiyue 2017-07-04 19:19:34 +08:00
if stations[key] == station_eg:
错误提示很明显啊 |
6
saulshao 2017-07-04 19:36:58 +08:00
def _get_keys(station_eg):
for key in stations: if stations[key] == station_eg: def pretty_print(self): 上面列出的代码,倒数第二行是个冒号,解释器会认为下一行应该是 If 条件成立的时候要执行的语句。 |
7
altboy 2017-07-04 19:50:19 +08:00
表示同情,肯定是排错排了两个小时然后没辙了才贴上来。。。
|
8
SlipStupig 2017-07-04 20:49:13 +08:00
你需要 pycharm....
|
9
ech0x 2017-07-04 21:04:28 +08:00 via iPad
或者 yapf
|
10
0xABCD 2017-07-04 21:06:28 +08:00 via Android
需要游标卡尺测量一下缩进
|
11
ytmsdy 2017-07-05 10:11:45 +08:00
@SlipStupig 我觉得他需要小黄鸭!
|