存在长度不均匀的txt文件
LS-DYNA user input
ls-dyna mpp.78769 s date 01/02/2013
constraint # axial shear time failure length rslt moment torsion
1720 8.39282E-01 6.55466E-01 1.20000E+03 0.0 spotweld beam ID 938970 4.47325E+01 2.24041E+00
1721 3.30134E-01 5.08016E-01 1.20000E+03 0.0 spotweld beam ID 938971 4.47310E+01 1.70857E+00
1722 9.52039E-01 2.24977E+00 1.20000E+03 0.0 spotweld beam ID 938972 3.50040E+00 1.14531E+01
1723 1.37947E+00 3.75614E+00 1.20000E+03 0.0 spotweld beam ID 938973 2.99986E+00 3.72429E+01
1724 -1.29900E+00 8.59783E-01 1.20000E+03 0.0 spotweld beam ID 938974 3.50112E+00 1.11357E+01
1725 -1.39978E+00 5.05035E+00 1.20000E+03 0.0 spotweld beam ID 938975 2.99934E+00 1.69379E+01
1726 -8.28811E-01 2.36767E+00 1.20000E+03 0.0 spotweld beam ID 938976 3.50022E+00 1.01569E+01
1727 -8.02390E-01 2.83158E+00 1.20000E+03 0.0 spotweld beam ID 938977 2.99945E+00 5.26153E+01
1728 2.45994E+01 2.55278E+02 1.20000E+03 0.0 spotweld beam ID 938978 3.51565E+00 1.03888E+01
1729 3.79365E+01 1.91420E+01 1.20000E+03 0.0 spotweld beam ID 938978 2.99987E+00 8.96939E+00不使用skiprows,因为没有数据的行会在不同的情况下发生变化,所以我尝试通过
pd.read_csv(File, header=None, delim_whitespace=True)它会抛出一个错误,比如
pandas.parser.CParserError: Error tokenizing data. C error: Expected 3 fields in line 2, saw 5然后,我重新定义pandas参数,如下所示
my_cols = ['A', 'B', 'C', 'D', 'E','F','G']
elout= pd.read_csv(File, names=my_cols, header=None, delim_whitespace=True)不会有任何问题。除了这种愚蠢的方式,有没有其他我可以用来解决这个问题的设置?
谢谢!
发布于 2017-01-14 07:05:59
如果您不想使用skiprows,另一种方法是像f = open(File)一样自己打开文件。然后手动f.readline()和解析您不感兴趣的第一行。一旦通过f提取了头的有用部分,并且文件指针到达了表的开头,只需将f作为第一个参数传递给read_csv,pandas将从该点开始处理数据。
https://stackoverflow.com/questions/41644777
复制相似问题