gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 08:11:35 25 4
gpt4 key购买 nike

我有一个像这样的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 数据框

特别是电台名称没有填满所有单元格,因此会出现问题

您能帮我解决一下吗?

最佳答案

此代码使用 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)

它生成以下 Pandas 数据框:

enter image description here

关于python - 将复杂格式的文本解析为python数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55218743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com