Python错误标记化数据:如何避免不同长度引起的错误
我正在尝试使用pandas read_csv函数读取*.dat文件Python错误标记化数据:如何避免不同长度引起的错误,python,pandas,tokenize,Python,Pandas,Tokenize,我正在尝试使用pandas read_csv函数读取*.dat文件 df = pd.read_csv(file, skiprows=0, header=None, sep=" ", parse_dates=[[0, 1]]) 数据如下所示: 2019-06-01 04:00:22 PW 100 2000 2000 /// // // // ////// ////// //// 2019-06-01 04:00:32 PW 100 2000 2000 /// // // // ///
df = pd.read_csv(file, skiprows=0, header=None, sep=" ", parse_dates=[[0, 1]])
数据如下所示:
2019-06-01 04:00:22 PW 100 2000 2000 /// // // // ////// ////// ////
2019-06-01 04:00:32 PW 100 2000 2000 /// // // // ////// ////// ////
2019-06-01 04:00:42 PW 100 2000 2000 /// // // // ////// ////// ////
2019-06-01 04:00:52 PW 100 2000 2000 /// // // // ////// ////// ////
2019-06-01 04:01:02 PW 100 2000 2000 /// // // // ////// ////// ////
2019-06-01 04:01:12 PW 100 2000 2000 /// // // // ////// ////// ////
2019-06-01 04:01:22 PW 100 2000 2000 /// // // // ////// ////// ////
2019-06-01 04:01:32 PW 100 2000 2000 /// // // // ////// ////// ////
我得到一个标记化错误:
ParserError: Error tokenizing data. C error: Expected 16 fields in line 242, saw 17
我认为这是错误的原因,因为在第242行中,第6列中的值比之前的行中的值低,例如第6列保持在2000或有4位数的值(例如1501),但在第242行中,它下降到991(三位数)
我怎样才能消除这个错误
error\u bad\u lines=False不是选项,因为我需要的正是这些值您应该使用sep=“+”
或sep=“\s+”
而不是sep=“
。对于后者,多个空格被分隔成多个空列,当空格数发生变化时会导致错误
或者,您可以指定
delim_whitespace=True
而不是sep
或delimiter
非常感谢!!通过添加sep=“+”它工作得非常好!我没有意识到这一点。你帮我节省了很多时间!非常感谢你
2019-06-01 04:39:32 PW 100 2000 2000 /// // // // ////// ////// ////
2019-06-01 04:39:42 PW 100 1501 2000 /// // // // ////// ////// ////
2019-06-01 04:39:52 PW 100 1501 2000 /// // // // ////// ////// ////
2019-06-01 04:40:02 PW 100 1501 2000 /// // // // ////// ////// ////
2019-06-01 04:40:12 PW 100 1187 2000 /// // // // ////// ////// ////
2019-06-01 04:40:22 PW 100 1187 2000 /// // // // ////// ////// ////
2019-06-01 04:40:32 PW 100 991 2000 /// // // // ////// ////// ////