我读取一个文件
1 [ 1s 1/2-1/2]+ 0.83 -66.379 -1.0000000
2 [ 1s 1/2 1/2]+ 0.83 -66.379 -1.0000000
3 [ 1s 1/2-1/2]+ 0.82 -61.930 1.0000000
4 [ 1s 1/2 1/2]+ 0.82 -61.930 1.0000000
5 [ 1p 3/2-1/2]- 0.73 -40.210 -1.0000000
6 [ 1p 3/2 1/2]- 0.77 -40.210 -1.0000000
7 [ 1p 3/2-3/2]- 0.76 -40.210 -1.0000000
8 [ 1p 3/2 3/2]- 0.64 -40.210 -1.0000000
通过以下方式:
spe=pd.read_csv("spe.dat",delimiter='s+',skiprows=[0,1])
spe.columns=['index','label','weight','ee','tz']
我收到错误消息:
ValueError: Length mismatch: Expected axis has 1 elements, new values have 5 elements
我意识到第二列(如'[ 1s 1/2-1/2]+'
(被读作三列。有没有办法将整个'[ 1s 1/2-1/2]+'
作为一列阅读?谢谢。
读取数据帧时未正确分隔列。我建议阅读 Python 正则表达式教程,了解如何将正则表达式用于分隔符。
columns = ['index','label','weight','ee','tz']
pd.read_csv('spe.dat', sep='s{2,}', names=columns, index_col=0, skiprows=[0, 1])
返回
label weight ee tz
index
1 [ 1s 1/2-1/2]+ 0.83 -66.379 -1.0
2 [ 1s 1/2 1/2]+ 0.83 -66.379 -1.0
3 [ 1s 1/2-1/2]+ 0.82 -61.930 1.0
4 [ 1s 1/2 1/2]+ 0.82 -61.930 1.0
5 [ 1p 3/2-1/2]- 0.73 -40.210 -1.0
6 [ 1p 3/2 1/2]- 0.77 -40.210 -1.0
7 [ 1p 3/2-3/2]- 0.76 -40.210 -1.0
8 [ 1p 3/2 3/2]- 0.64 -40.210 -1.0