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

如何在Python中打开.stg文件?,python,pandas,txt,Python,Pandas,Txt,我在Spyder中使用Python3.8读取扩展名为“.stg”的数据文件。这些文件是地球物理中使用的一些设备的输出。到目前为止,我找到的唯一选项是在文本编辑器(记事本)中手动打开文件,将其另存为.txt,最后使用以下代码行: 将熊猫作为pd导入 df=pd.read\u csv('TSC16.txt',header=3) 文件可以很好地打开,但我想知道是否有一种有效的方法来打开它,因为最终,我的目标是处理大量的.stg文件。 谢谢你的帮助 STG文件是一个纯文本文件,它有三行元数据,然后是一个

我在Spyder中使用Python3.8读取扩展名为“.stg”的数据文件。这些文件是地球物理中使用的一些设备的输出。到目前为止,我找到的唯一选项是在文本编辑器(记事本)中手动打开文件,将其另存为.txt,最后使用以下代码行:
将熊猫作为pd导入
df=pd.read\u csv('TSC16.txt',header=3)
文件可以很好地打开,但我想知道是否有一种有效的方法来打开它,因为最终,我的目标是处理大量的.stg文件。
谢谢你的帮助

STG文件是一个纯文本文件,它有三行元数据,然后是一个逗号分隔的值表。您可以使用pd阅读此内容。按您所掌握的方式阅读。您需要设置列名,您可以找到这些列名。我已经将它们包含在下面的代码中

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

STG文件是一个纯文本文件,它有三行元数据,然后是一个逗号分隔的值表。您可以使用pd阅读此内容。按您所掌握的方式阅读。您需要设置列名,您可以找到这些列名。我已经将它们包含在下面的代码中

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

Python不关心文件的名称。@KarlKnechtel True,但没有帮助。@KarlKnechtel Jeje现在我知道了!我之所以发布此消息,是因为每次我尝试使用“.stg”而不是“.txt”时,我都收到一个控制台错误:“文件.stg不存在”可能是我在错误的目录中或类似的地方……Python不关心文件的名称。@KarlKnechtel True,但没有帮助。@KarlKnechtel Jeje现在我知道了!我发布这篇文章是因为每次我尝试使用“.stg”而不是“.txt”时,我都会收到一个控制台错误:“文件.stg不存在”可能是我在错误的目录中或类似的地方。。。