如何在 Python 中打开 .stg 文件?
How to open a .stg file in Python?
我在 Spyder 中使用 Python 3.8 来读取扩展名为“.stg”的数据文件。这个文件是地球物理学See file 中使用的一些设备的输出。到目前为止我找到的唯一选择是在文本编辑器(记事本)中手动打开文件,将其另存为.txt,最后使用这行代码:
import pandas as pd
df = pd.read_csv('TSC16.txt', header=3)
该文件可以正常打开,但我想知道是否有一种有效的方法来执行此操作,因为最终,我的目标是处理大量此类 .stg 文件。
谢谢你的帮助!
STG 文件是一个纯文本文件,包含三行元数据,然后是 comma-separated 个值 table。您可以使用 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] = 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
我在 Spyder 中使用 Python 3.8 来读取扩展名为“.stg”的数据文件。这个文件是地球物理学See file 中使用的一些设备的输出。到目前为止我找到的唯一选择是在文本编辑器(记事本)中手动打开文件,将其另存为.txt,最后使用这行代码:
import pandas as pd
df = pd.read_csv('TSC16.txt', header=3)
该文件可以正常打开,但我想知道是否有一种有效的方法来执行此操作,因为最终,我的目标是处理大量此类 .stg 文件。
谢谢你的帮助!
STG 文件是一个纯文本文件,包含三行元数据,然后是 comma-separated 个值 table。您可以使用 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] = 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