将复杂格式的文本解析为python数据表

时间:2019-03-18 10:00:40

标签: python pandas dataframe beautifulsoup

我有一个这样的txt文件

 Station coordinates and velocities:
  ----------------------------------
  Reference epoch: 2017-01-02 12:00:00

  Station name          Typ   A priori value  Estimated value    Correction     RMS error      3-D ellipsoid        2-D ellipse
  ------------------------------------------------------------------------------------------------------------------------------------
  CHNG 99128            X     -3233224.72062   -3233224.72062       0.00000       0.00120
                        Y      4067916.18933    4067916.18933       0.00000       0.00118
                        Z      3686212.02917    3686212.02917       0.00000       0.00091

                        U           61.78989         61.78989       0.00000       0.00160     0.00161    7.0
                        N         35.5333100       35.5333100       0.00000       0.00046     0.00044    1.8     0.00046    3.2
                        E        128.4780652      128.4780652       0.00000       0.00095     0.00094   -5.4     0.00095

  CHSG 98109            X     -3237146.18442   -3237146.18442       0.00000       0.00119
                        Y      3989513.50556    3989513.50556       0.00000       0.00130
                        Z      3767338.46367    3767338.46367       0.00000       0.00096

                        U          250.04930        250.04930       0.00000       0.00170     0.00170    5.4
                        N         36.4356270       36.4356270       0.00001       0.00048     0.00045    0.6     0.00048    0.6
                        E        129.0563506      129.0563506       0.00000       0.00095     0.00095    0.0     0.00095

  CHWN 09131            X     -3260411.69912   -3260411.69912       0.00000       0.00127
                        Y      4070678.22490    4070678.22490       0.00000       0.00143
                        Z      3659345.19301    3659345.19301       0.00000       0.00103

                        U           88.37577         88.37577       0.00000       0.00187     0.00188    5.0
                        N         35.2361197       35.2361197       0.00000       0.00048     0.00046    0.5     0.00048    0.3
                        E        128.6929994      128.6929994       0.00000       0.00098     0.00098   -0.6     0.00098

但是我真的不知道如何将其更改为python数据框

尤其是电台名称未填满所有单元格,因此出现了问题

您能帮我解决一个问题吗?

1 个答案:

答案 0 :(得分:1)

此代码使用Pandas.read_fwf()按位置读取文件,跳过前6行,删除空白行,并用fill_na()填充空白行的桩号:

cols = [(3,23),(24,30),(30,45),(46,62),(63,76),(77,90), (91,102), (103, 109), (110,121), (122,128)]
names = ['Station name','Typ', 'A priori value', 'Estimated value', 'Correction', 'RMS error', '3-D', 'ellipsoid', '2-D', 'ellipse']
df = pd.read_fwf('my_file_24.txt', header=None, colspecs = cols, names = names, skiprows = 6)
df = df[~df.Typ.isnull()]
df['Station name'].fillna(method='ffill',inplace = True)

它将生成以下熊猫数据框:

enter image description here