简体   繁体   English

Python IndexError:出了什么问题?

[英]Python IndexError: what's wrong?

I have to search a string containing dates, so i have to find substrings that match formats (date_text1, date_text2 and date_text) and munge them into a conventional format such as 25/04/1955.我必须搜索包含日期的字符串,因此我必须找到匹配格式(date_text1、date_text2 和 date_text)的子字符串,并将它们转换为传统格式,例如 25/04/1955。

Why does the index not exist?为什么索引不存在?

import re 

#date_text1 = '042555'
#date_text2 = '04/25/1955'
date_text = 'April 25, 1955'

date_patterns = (r'(\d{2}) (\d{2}) (\d{2})', r'(\d{2}) / (\d{2}) / (\d{4})', r'([\w\D]+) (\d{2}) , (\d{4})')

scan = True
idx = 0
while scan:
    match = re.fullmatch(date_patterns[idx], date_text)
    if match:
        month, day, year = match.groups()
        scan = False
    else:
        idx += 1

# Adjust for years
if int(year) <= 19:
    year = "20"+year
elif int(year) <= 99:
    year = "19"+year

# Adjust for months
months = {"January":"01", "February":"02", "March":"03", "April":"04", "May":"05", "June":"06","July":"07", "August":"08", "September":"09", "October":"10","November":"11", "December":"12"}
if len(month) > 2:
    month = months[month]

normalized_date = f'{day}/{month}/{year}'
print(normalized_date)

Because none of your patterns ever match, the loop will keep incrementing the idx until it goes over the bounds of the tuple.因为你的模式都不匹配,循环将不断增加idx直到它超过元组的边界。

The reason for no match, is that you have a small error in your last regex pattern.不匹配的原因是你的最后一个正则表达式模式有一个小错误。 This:这个:

r'([\w\D]+) (\d{2}) , (\d{4})')

should be:应该:

r'([\w\D]+) (\d{2}), (\d{4})')

After fixing this I'm able get the correct output from the program:解决这个问题后,我可以从程序中获得正确的输出:

 25/04/1955

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM