V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
1002149271
V2EX  ›  Python

django 运行 Python 无法访问

  •  
  •   1002149271 · 2017-07-04 18:31:55 +08:00 · 2820 次点击
    这是一个创建于 2488 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我直接把代码移进的 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

    11 条回复    2017-07-05 10:11:45 +08:00
    lxy
        1
    lxy  
       2017-07-04 18:39:35 +08:00
    错误提示很明白了,再认真看一下吧
    arischow
        2
    arischow  
       2017-07-04 18:40:49 +08:00 via iPhone
    错误提示写明白了……你需要一个 IDE
    1002149271
        3
    1002149271  
    OP
       2017-07-04 18:59:19 +08:00
    @lxy 但是我这个缩进不是正常的吗
    ysicing
        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()
    ```
    cszhiyue
        5
    cszhiyue  
       2017-07-04 19:19:34 +08:00
    if stations[key] == station_eg:

    错误提示很明显啊
    saulshao
        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 条件成立的时候要执行的语句。
    altboy
        7
    altboy  
       2017-07-04 19:50:19 +08:00
    表示同情,肯定是排错排了两个小时然后没辙了才贴上来。。。
    SlipStupig
        8
    SlipStupig  
       2017-07-04 20:49:13 +08:00
    你需要 pycharm....
    ech0x
        9
    ech0x  
       2017-07-04 21:04:28 +08:00 via iPad
    或者 yapf
    0xABCD
        10
    0xABCD  
       2017-07-04 21:06:28 +08:00 via Android
    需要游标卡尺测量一下缩进
    ytmsdy
        11
    ytmsdy  
       2017-07-05 10:11:45 +08:00
    @SlipStupig 我觉得他需要小黄鸭!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   909 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 57ms · UTC 22:01 · PVG 06:01 · LAX 15:01 · JFK 18:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.