python - 解析导致 ValueError 的日期时间
问题描述
我试图解析 CSV 文件的时间戳(第一列名为“时间”)。时间戳的格式为:01.10.2016 00:10:00
( dd.mm.yyyy HH:MM:SS
)
timestamp_parser = lambda x: pd.datetime.strptime(x, "%d.%m.%Y %H:%M:%S")
df_pi_data = pd.read_csv( "pi_daten.csv", usecols = (0,1), sep =';', thousands='.', decimal=',', names = ['time','temperature'], parse_dates=['time'], date_parser = timestamp_parser)
出现以下错误:
ValueError: time data '\xef\xbb\xbftime' does not match format '%d.%m.%Y %H:%M:%S'
@kantal:
time;temperature;
01.10.2016 00:00; 23,13854599;
01.10.2016 00:10; 23,24945831;
01.10.2016 00:20; 23,16853714;
解决方案
time;temperature
01.10.2016 00:00; 23,13854599
01.10.2016 00:10; 23,24945831
01.10.2016 00:20; 23,16853714
import pandas as pd
timestamp_parser = lambda x: pd.datetime.strptime(x, "%d.%m.%Y %H:%M")
df = pd.read_csv("test.txt", sep=";", decimal=',', \
parse_dates=['time'], date_parser = timestamp_parser)
我使用这些数据,并使用代码,成功工作
time temperature
0 2016-10-01 00:00:00 23.138546
1 2016-10-01 00:10:00 23.249458
2 2016-10-01 00:20:00 23.168537
推荐阅读
- flutter - 使用 BottomNavigationBar 在屏幕之间切换时,Flutter 中出现“'!_needsLayout': is not true”错误
- r - 如何使用代码在 R Shiny 中触发 session$onSessionEnded?
- wordpress - 我的 wordpress 网站已关闭,如何解决此错误
- android - 读取发布者账号配置失败;请检查您配置的应用 ID。收到的应用 ID:`ca-app-pub-3940256099942544~3347511713`
- javascript - document.querySelectorAll.forEach 奇怪的行为
- c++ - 在返回内存的 DLL 函数中,在哪里分配和在哪里释放?
- android - FCM 注册期间出错。请设置您的项目ID
- asp.net-core - 错误 CS0234 在 asp.net 核心的命名空间“System.Web”中不存在类型或命名空间名称“HttpContext”
- coordinates - 获取以 180 度穿过 180 度子午线的两点之间的点
- python - 在 Pandas 中按 ID 分组的数据框中减去每年的连续行