修改 dateutil.parser.parse 参数以更正日期错误识别
Modify dateutil.parser.parse parameters to correct date misidentification
尽管修改了 parseinfo.JUMP 属性
,但 Dateutil 解析错误地将字符串识别为日期
我正在制作一个函数来检测某个值是否为日期(给定各种不同的格式)。 dateutil.parser.parse 非常适合这个,但它错误地将我的一些数据识别为日期。
具体来说就是将'ad-3'修正为datetime.datetime(2019, 10, 3, 0, 0),即当前年月3日。
我试过修改 parseinfo 以排除 'ad'
根据 dateutil 的文档,parse 命令采用 parseinfo 参数。
我认为我的问题是因为传递给解析的默认解析信息 class 在其跳转 属性 中包含 'ad'。我尝试实例化自己的parseinfo class,修改跳转列表属性 并将其传递给parse,但问题仍然存在。
from dateutil import parser
def check_format(val, pi = parser.parserinfo()):
# Check date
try:
parser.parse(val, parserinfo=pi)
return 'date'
except ValueError:
return 'Not date'
default_pi = parser.parserinfo()
my_pi = parser.parserinfo()
my_pi.JUMP = [j for j in my_pi.JUMP if j not in ['ad']]
print('Default JUMP List:')
print(default_pi.JUMP) # Print the default JUMP list and you can se ad is part of the list
print('My Corrected JUMP List')
print(my_pi.JUMP) # Print the modified JUMP list and you see that we have successfully excluded
print('Return using default JUMP list:')
print(check_format('ad-3')) # Using default parserinfo
print('Return using my JUMP list:')
print(check_format('ad-3', my_pi)) # Using my parserinfo
print('Control test with a normal string:')
print(check_format('sad-3', my_pi))
问题:
check_format('ad-3', my_pi)
returns 'date' 尽管传递的 parserinfo 实例将 'ad' 从其列表中排除。
作为对照,我传递了一个类似的字符串 'sad-3' 并且输出符合预期:'Not date'。
输出:
默认跳转列表:
[' ', '.', ',', ';', '-', '/', "'", 'at', 'on', 'and', 'ad', 'm', 't', 'of', 'st', 'nd', 'rd', 'th']
我更正的跳转列表
[' ', '.', ',', ';', '-', '/', "'", 'at', 'on', 'and', 'm', 't', 'of', 'st', 'nd', 'rd', 'th']
Return 使用默认 JUMP 列表:
日期
Return 使用我的 JUMP 列表:
日期
用普通字符串控制测试:
不是日期
我正在谷歌搜索其他内容,但偶然发现了你的问题,所以我会回答它。
PI
等是class属性。你只需要按正确的顺序实例化它。
from dateutil import parser
def check_format(val, pi):
# Check date
try:
parser.parse(val, parserinfo=pi())
return 'date'
except ValueError:
return 'Not date'
default_pi = parser.parserinfo
my_pi = parser.parserinfo
my_pi.JUMP = [j for j in my_pi.JUMP if j not in ['ad']]
print('Default JUMP List:')
print(default_pi.JUMP) # Print the default JUMP list and you can se ad is part of the list
print('My Corrected JUMP List')
print(my_pi.JUMP) # Print the modified JUMP list and you see that we have successfully excluded
print('Return using default JUMP list:')
print(check_format('ad-3', default_pi)) # Using default parserinfo
print('Return using my JUMP list:')
print(check_format('ad-3', my_pi)) # Using my parserinfo
print('Control test with a normal string:')
print(check_format('sad-3', my_pi))
check_format('ad-3', my_pi) returns 'date' despite a parserinfo instance is passed that excludes 'ad' from its list.
现在 returns Not date
.
尽管修改了 parseinfo.JUMP 属性
,但 Dateutil 解析错误地将字符串识别为日期我正在制作一个函数来检测某个值是否为日期(给定各种不同的格式)。 dateutil.parser.parse 非常适合这个,但它错误地将我的一些数据识别为日期。
具体来说就是将'ad-3'修正为datetime.datetime(2019, 10, 3, 0, 0),即当前年月3日。
我试过修改 parseinfo 以排除 'ad'
根据 dateutil 的文档,parse 命令采用 parseinfo 参数。
我认为我的问题是因为传递给解析的默认解析信息 class 在其跳转 属性 中包含 'ad'。我尝试实例化自己的parseinfo class,修改跳转列表属性 并将其传递给parse,但问题仍然存在。
from dateutil import parser
def check_format(val, pi = parser.parserinfo()):
# Check date
try:
parser.parse(val, parserinfo=pi)
return 'date'
except ValueError:
return 'Not date'
default_pi = parser.parserinfo()
my_pi = parser.parserinfo()
my_pi.JUMP = [j for j in my_pi.JUMP if j not in ['ad']]
print('Default JUMP List:')
print(default_pi.JUMP) # Print the default JUMP list and you can se ad is part of the list
print('My Corrected JUMP List')
print(my_pi.JUMP) # Print the modified JUMP list and you see that we have successfully excluded
print('Return using default JUMP list:')
print(check_format('ad-3')) # Using default parserinfo
print('Return using my JUMP list:')
print(check_format('ad-3', my_pi)) # Using my parserinfo
print('Control test with a normal string:')
print(check_format('sad-3', my_pi))
问题:
check_format('ad-3', my_pi)
returns 'date' 尽管传递的 parserinfo 实例将 'ad' 从其列表中排除。
作为对照,我传递了一个类似的字符串 'sad-3' 并且输出符合预期:'Not date'。
输出:
默认跳转列表: [' ', '.', ',', ';', '-', '/', "'", 'at', 'on', 'and', 'ad', 'm', 't', 'of', 'st', 'nd', 'rd', 'th']
我更正的跳转列表 [' ', '.', ',', ';', '-', '/', "'", 'at', 'on', 'and', 'm', 't', 'of', 'st', 'nd', 'rd', 'th']
Return 使用默认 JUMP 列表: 日期
Return 使用我的 JUMP 列表: 日期
用普通字符串控制测试: 不是日期
我正在谷歌搜索其他内容,但偶然发现了你的问题,所以我会回答它。
PI
等是class属性。你只需要按正确的顺序实例化它。
from dateutil import parser
def check_format(val, pi):
# Check date
try:
parser.parse(val, parserinfo=pi())
return 'date'
except ValueError:
return 'Not date'
default_pi = parser.parserinfo
my_pi = parser.parserinfo
my_pi.JUMP = [j for j in my_pi.JUMP if j not in ['ad']]
print('Default JUMP List:')
print(default_pi.JUMP) # Print the default JUMP list and you can se ad is part of the list
print('My Corrected JUMP List')
print(my_pi.JUMP) # Print the modified JUMP list and you see that we have successfully excluded
print('Return using default JUMP list:')
print(check_format('ad-3', default_pi)) # Using default parserinfo
print('Return using my JUMP list:')
print(check_format('ad-3', my_pi)) # Using my parserinfo
print('Control test with a normal string:')
print(check_format('sad-3', my_pi))
check_format('ad-3', my_pi) returns 'date' despite a parserinfo instance is passed that excludes 'ad' from its list.
现在 returns Not date
.