首页 > 解决方案 > 如何在 Python 中打开 .stg 文件?

问题描述

我在 Spyder 中使用 Python 3.8 来读取扩展名为“.stg”的数据文件。这些文件是地球物理学中使用的一些设备的输出参见文件。到目前为止我发现的唯一选项是在文本编辑器(记事本)中手动打开文件,将其保存为.txt,最后使用这行代码: import pandas as pd df = pd.read_csv('TSC16.txt', header=3) 文件打开很好,但我想知道是否有一种有效的方法可以做到这一点,因为最后,我的目标是使用大量的 .stg 文件。谢谢你的帮助!

标签: pythonpandastxt

解决方案


STG 文件是一个纯文本文件,其中包含三行元数据和一个逗号分隔值表。您可以按照自己的方式阅读此pd.read_csv内容。您需要设置列名,您可以在此处找到。我已将它们包含在下面的代码中。

import pandas as pd

columns = [
    'data record number',
    'USER',
    'date (YYYYMMDD)',
    'time (hh:mm:ss)',
    'V/I',
    '% error in tenths of percent',
    'output current in mA',
    'apparent resistivity in Ωm or Ωft',
    'command file identifier',
    'X-coordinate for the A-electrode',
    'Y-coordinate for the A-electrode',
    'Z-coordinate for the A-electrode',
    'X-coordinate for the B-electrode',
    'Y-coordinate for the B-electrode',
    'Z-coordinate for the B-electrode',
    'X-coordinate for the M-electrode',
    'Y-coordinate for the M-electrode',
    'Z-coordinate for the M-electrode',
    'X-coordinate for the N-electrode',
    'Y-coordinate for the N-electrode',
    'Z-coordinate for the N-electrode',
    'Cmd line number',
    'Transmitter volt code',
    '# of measurement cycles',
    'Measurement time used',
    'Gain setting',
    'Channel used',
]

df = pd.read_csv("TSC16.stg", skiprows=3, header=None, names=columns)

必须对数据帧进行一些处理。两列有尾随空格,六列包括前缀,如cmd=. 下面的代码处理了这两件事。

# Strip whitespace from some columns.
for colname in ["USER", "command file identifier"]:
    df.loc[:, colname] = df.loc[:, colname].str.strip()

# Remove prefixes like 'cmd=' and convert to a numeric type.
for colname in ["Cmd line number", "Transmitter volt code", "# of measurement cycles",
       "Measurement time used", "Gain setting", "Channel used"]:
    df.loc[:, colname] = df.loc[:, colname].str.split("=", expand=True).iloc[:, -1].astype(float)

这是此处理后数据帧的第一行:

data record number                            1
USER                                       USER
date (YYYYMMDD)                        20190611
time (hh:mm:ss)                        11:09:32
V/I                                  0.00510117
% error in tenths of percent                  0
output current in mA                        361
apparent resistivity in Ωm or Ωft      0.153848
command file identifier                   TSC16
X-coordinate for the A-electrode            1.6
Y-coordinate for the A-electrode              0
Z-coordinate for the A-electrode              0
X-coordinate for the B-electrode              0
Y-coordinate for the B-electrode              0
Z-coordinate for the B-electrode              0
X-coordinate for the M-electrode            3.2
Y-coordinate for the M-electrode              0
Z-coordinate for the M-electrode              0
X-coordinate for the N-electrode            4.8
Y-coordinate for the N-electrode              0
Z-coordinate for the N-electrode              0
Cmd line number                               1
Transmitter volt code                        32
# of measurement cycles                       1
Measurement time used                      14.4
Gain setting                                200
Channel used                                  1
Name: 0, dtype: object

请注意,将文件扩展名更改为.txt不会更改文件的内容。您可以在任何文本编辑器中打开 STG 文件以查看其内容。为了清楚起见,这里是文件的前几行。

Advanced Geosciences, Inc. SuperSting R8-IP Resistivity meter. S/N: SS1601074 Type: 3D
Firmware version: 01.33.74E Survey period: 20190611 Records: 2072
Unit: meter
   1,USER   ,20190611,11:09:32, 5.10117E-03,   0,361, 1.53848E-01,TSC16    , 1.60000E+00, 0.00000E+00, 0.00000E+00, 0.00000E+00, 0.00000E+00, 0.00000E+00, 3.20000E+00, 0.00000E+00, 0.00000E+00, 4.80000E+00, 0.00000E+00, 0.00000E+00,Cmd=1,HV=32,Cyk=1,MTime=14.4,Gain=200,Ch=1
   2,USER   ,20190611,11:09:32, 1.39790E-03,   0,361, 1.68638E-01,TSC16    , 1.60000E+00, 0.00000E+00, 0.00000E+00, 0.00000E+00, 0.00000E+00, 0.00000E+00, 4.80000E+00, 0.00000E+00, 0.00000E+00, 6.40000E+00, 0.00000E+00, 0.00000E+00,Cmd=1,HV=32,Cyk=1,MTime=14.4,Gain=200,Ch=2

推荐阅读