使用内置的csv 模块,
import io
import csv
import datetime
# This stands in for `f = open("something.csv")` to make
# this example self-contained.
f = io.StringIO("""
1.727783203125,2022-09-15T18:18:12.987
1.927783203125,2022-09-15T19:18:12
""".strip())
# Iterate over CSV rows, parse the first column as float and the second as a datetime.
data = [(float(x), datetime.datetime.fromisoformat(y)) for x, y in csv.reader(f)]
print(data)
打印出来
[
(1.727783203125, datetime.datetime(2022, 9, 15, 18, 18, 12, 987000)),
(1.927783203125, datetime.datetime(2022, 9, 15, 19, 18, 12, 0))
]
你也可以使用 Pandas,但如果这真的是你所需要的,那就有点矫枉过正了。