如何在Python中打开.stg文件?

问题描述

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

解决方法

STG文件是纯文本文件,具有三行元数据,然后是逗号分隔的值表。您可以随时使用pd.read_csv阅读。您将需要设置列名称,您可以找到here。我已将它们包含在下面的代码中。

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].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,361,1.53848E-01,TSC16,1.60000E+00,0.00000E+00,3.20000E+00,4.80000E+00,Cmd=1,HV=32,Cyk=1,MTime=14.4,Gain=200,Ch=1
   2,1.39790E-03,1.68638E-01,6.40000E+00,Ch=2