Demo entry 6363429

py

   

Submitted by anonymous on May 13, 2017 at 09:27
Language: Python. Code size: 2.0 kB.

def re_format_data(date, start, end, type):
    url = 'https://kyfw.12306.cn/otn/leftTicket/query?leftTicketDTO.train_date={}&leftTicketDTO.from_station={}&leftTicketDTO.to_station={}&purpose_codes={}'.format(
        date, start, end, type
    )
    try:
        r = requests.get(url, timeout=7, verify=False)
        getdatadict = r.json()['data']
        datalist = getdatadict['result']
    except Exception as e:
        print e
        return None
    datalist_fmt = [string2dict(mystr) for mystr in datalist]
    out_dict = {"data": datalist_fmt}
    return out_dict




def string2dict(mystr):
    split_list = mystr.split('|')

    def checkempty(string):
        if(string == ''):
            return '--'
        else:
            return string
    DTO = {
        "station_train_code": split_list[3],
        "start_station_telecode": split_list[4],
        "end_station_telecode": split_list[5],
        "from_station_telecode": split_list[6],
        "to_station_telecode": split_list[7],
        "fromStationName": reverse_placemap[split_list[6]],
        "toStationName": reverse_placemap[split_list[7]],
        "start_time": split_list[8],
        "arrive_time": split_list[9],
        "lishi":split_list[10],
        "start_train_date": split_list[13],
        "gg_num": checkempty(split_list[20]),
        "gr_num": checkempty(split_list[21]),
        "qt_num": checkempty(split_list[22]),
        "rw_num": checkempty(split_list[23]),
        "rz_num": checkempty(split_list[24]),
        "tz_num": checkempty(split_list[25]),
        "wz_num": checkempty(split_list[26]),
        "yb_num": checkempty(split_list[27]),
        "yw_num": checkempty(split_list[28]),
        "yz_num": checkempty(split_list[29]),
        "ze_num": checkempty(split_list[30]),
        "zy_num": checkempty(split_list[31]),
        "swz_num": checkempty(split_list[32])
    }
    out_dic = {
            "queryLeftNewDTO": DTO
        }
    return out_dic

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).