Skip to content

Instantly share code, notes, and snippets.

@net21
Last active December 13, 2016 04:55
Show Gist options
  • Save net21/8279367 to your computer and use it in GitHub Desktop.
Save net21/8279367 to your computer and use it in GitHub Desktop.
解析kyfw.12306.cn返回的余票字符串
def getTicketCount(yp_info):
"""
从余票字符串中获得余票数据。
余票信息字符串如下:
1018053038405095000010180500003032150000
O055300502M0933000989174800019
返回的是一个余票字典,并不是很全面,只取了我需要的类型,如下
ZY 一等座
ZE 二等座
WZ 无座
YZ 硬座
YW 硬卧
RW 软卧
"""
seat = {"WZ":0, "YZ":0, "YW":0, "RW":0, "ZY":0, "ZE":0}
i = 0;
while i in range(len(yp_info)):
# 1*****3179
group = yp_info[i:i+10];
# 1(席别)
seatType = group[:1];
# 硬座: 1, 硬卧: 3, 软卧: 4, 二等座:O, 一等座:M
# 3179
count = group[6:10];
while len(count) > 1 and count[0:1] == "0": # count为0000的情况, count最终等于0
count = count[1:1+len(count)];
# 3179
count = int(count);
if seatType == "1":
if count < 3000:
seat['YZ'] = count
else:
seat['WZ'] = count - 3000
elif seatType == "3":
seat['YW'] = count
elif seatType == "4":
seat['RW'] = count
elif seatType == "M":
seat['ZY'] = count
elif seatType == "O":
if count < 3000:
seat['ZE'] = count
else:
seat['WZ'] = count - 3000
i = i + 10
return seat
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment