Python 从 CSV 获取数据类型 - 日期时间和时间作为"2305"



如何从csv文件中获取日期时间和时间的数据类型:

with open(dataPath, newline='') as csvData:
reader = csv.DictReader(csvData, delimiter=';')
head = next(reader)
.
.

我有以下csv文件:

"...."; "YEAR"; "TIME"; "..." --> head
"...."; "19.05.2020"; "0050"; "..." --> "0050" means 00:50
"...."; "12.05.2020"; "2035"; "..." --> "2035" means 20:35

我尝试了一个功能:

def convert(value):
heuristics = [lambda value: datetime.strptime(value, "%d.%m.%Y"), datetime.strptime(value, "%H%D"), int, float]
for type in heuristics:
try:
return type(value)
except ValueError:
continue
# All other heuristics failed it is a string
return value

如果我有一个值"0";[1,2,3]";它输出一个valueerror,但为此我写了除了valueerror:continue,为什么当这个错误发生时它不继续,和";513165";

感谢

如果你想让它发挥作用,有几件事:

  • 两个strptime操作都需要lambda
  • %D不是有效的strptime指令,我想你指的是%M
  • 不要重新定义内置的type
  • 更好地捕捉并打印错误,以了解发生了什么

一些测试的工作示例(不要假设任何可能的错误都被完全覆盖…(

def convert(value):
heuristics = [lambda value: datetime.strptime(value, "%d.%m.%Y"),
lambda value: datetime.strptime(value, "%H%M"),
int,
float]
for f in heuristics:
try:
return f(value)
except ValueError as e:
print(f"encountered error: {e}")
continue
# All other heuristics failed it is a string
return value

for v in ('2355', 'asdf', '12', '3.14', '7.12.2013'):
print(f"testing '{v}'...")
result = convert(v)
print(result, type(result))

给你

testing '2355'...
encountered error: time data '2355' does not match format '%d.%m.%Y'
1900-01-01 23:55:00 <class 'datetime.datetime'>
testing 'asdf'...
encountered error: time data 'asdf' does not match format '%d.%m.%Y'
encountered error: time data 'asdf' does not match format '%H%M'
encountered error: invalid literal for int() with base 10: 'asdf'
encountered error: could not convert string to float: 'asdf'
asdf <class 'str'>
testing '12'...
encountered error: time data '12' does not match format '%d.%m.%Y'
1900-01-01 01:02:00 <class 'datetime.datetime'>
testing '3.14'...
encountered error: time data '3.14' does not match format '%d.%m.%Y'
encountered error: time data '3.14' does not match format '%H%M'
encountered error: invalid literal for int() with base 10: '3.14'
3.14 <class 'float'>
testing '7.12.2013'...
2013-12-07 00:00:00 <class 'datetime.datetime'>

最新更新