Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pandas以不均匀的行长度作为标题读取\u csv_Python_Pandas - Fatal编程技术网

Python Pandas以不均匀的行长度作为标题读取\u csv

Python Pandas以不均匀的行长度作为标题读取\u csv,python,pandas,Python,Pandas,有一个长度不均匀的txt文件 LS-DYNA user input ls-dyna mpp.78769 s date 01/02/2013 constraint # axial shear time failure

有一个长度不均匀的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)
没有问题。除了这种愚蠢的方式,还有其他设置可以解决这个问题吗


谢谢大家!

如果您不想使用
skiprows
,另一种方法是自己打开文件,如
f=open(file)
。然后您
f.readline()
并手动解析您不感兴趣的第一行。一旦您通过
f
提取了标题的有用部分,并且文件指针到达了表的开头,只需将
f
作为第一个参数传递给
read\u csv
,pandas将从该点开始处理数据。

如果您不想使用
skiprows
,另一种方法是自己打开文件,如
f=open(file)
。然后您
f.readline()
并手动解析您不感兴趣的第一行。一旦您通过
f
提取了标题的有用部分,并且文件指针到达了表的开头,只需将
f
传递到
read_csv
作为第一个参数,pandas将从该点开始处理数据