英文:
Parse formatted string in Golang
问题
我正在尝试使用Golang解析GNSS RINEX文件。
例如,这是VERSION行的RINEX规范:
+--------------------+------------------------------------------+------------+
|RINEX VERSION / TYPE| - 格式版本 (2.11)                          | F9.2,11X,  |
|                    | - 文件类型 ('O' 表示观测数据)                |   A1,19X,  |
|                    | - 卫星系统: 空白或 'G': GPS                  |   A1,19X   |
|                    |                     'R': GLONASS            |            |
|                    |                     'S': Geostationary      |            |
|                    |                          signal payload     |            |
|                    |                     'E': Galileo            |            |
|                    |                     'M': Mixed              |            |
+--------------------+------------------------------------------+------------+
RINEX文件中的每一行都有固定的宽度为80个ASCII字符 + "\n"。在这个例子中,前9个字符表示版本号(浮点数)。
在Python中,我可以使用:
struct.unpack("9s11s1s19s1s19s20s", line)
这将返回一个包含7个字符串的元组。
我是Go的新手,一直在尝试使用fmt.Sscanf来读取格式化文本:
func main() {
    str := "     2.11           OBSERVATION DATA    G (GPS)             RINEX VERSION / TYPE\n"
    var value float32
    a, err := fmt.Sscanf(str,"%9.2f", &value)
    fmt.Println(a)
    fmt.Println(err)
    fmt.Println(value)
}
返回:
0
bad verb %. for float32
0
Go中是否有任何包允许解析固定宽度的数据?
如果没有,编写类似于Python的struct的东西可能是一个好方法吗?
英文:
I'm trying to parse a GNSS RINEX file using Golang.
For example, here's the RINEX specification for the VERSION line:
+--------------------+------------------------------------------+------------+
|RINEX VERSION / TYPE| - Format version (2.11)                  | F9.2,11X,  |
|                    | - File type ('O' for Observation Data)   |   A1,19X,  |
|                    | - Satellite System: blank or 'G': GPS    |   A1,19X   |
|                    |                     'R': GLONASS         |            |
|                    |                     'S': Geostationary   |            |
|                    |                          signal payload  |            |
|                    |                     'E': Galileo         |            |
|                    |                     'M': Mixed           |            |
+--------------------+------------------------------------------+------------+
Each line in a RINEX file has a fixed width of 80 ASCII characters + "\n". In this example the first 9 characters represent the version number (float).
In Python I might use:
struct.unpack("9s11s1s19s1s19s20s", line)
which would return a tuple with 7 strings.
I'm new to go and have been trying to use fmt.Sscanf for reading formatted text:
func main() {
    str := "     2.11           OBSERVATION DATA    G (GPS)             RINEX VERSION / TYPE\n"
    var value float32
    a, err := fmt.Sscanf(str,"%9.2f", &value)
    fmt.Println(a)
    fmt.Println(err)
    fmt.Println(value)
}
returns:
0
bad verb %. for float32
0
Is there any package in go that permits parsing of fixed width data?
And if not, what might be a good approach for writing something similar to Python's struct?
答案1
得分: 4
例如,
package main
import (
    "errors"
    "fmt"
    "strconv"
    "strings"
)
// http://gage14.upc.es/gLAB/HTML/LaunchHTML.html
// http://gage14.upc.es/gLAB/HTML/Observation_Rinex_v2.11.html
func parseVersionType(line string) (version float64, fileType, satellite, label string, err error) {
    label = line[60:80]
    if label != "RINEX VERSION / TYPE" {
        err = errors.New("未知的头标签")
        return
    }
    version, err = strconv.ParseFloat(strings.TrimSpace(line[0:9]), 64)
    if err != nil {
        return
    }
    fileType = line[20:21]
    satellite = line[40:41]
    return
}
func main() {
    line := "     2.11           OBSERVATION DATA    G (GPS)             RINEX VERSION / TYPE\n"
    version, fileType, satellite, label, err := parseVersionType(line)
    fmt.Printf("%g %q %q %q %v\n", version, fileType, satellite, label, err)
}
输出:
2.11 "O" "G" "RINEX VERSION / TYPE" <nil>
英文:
For example,
package main
import (
	"errors"
	"fmt"
	"strconv"
	"strings"
)
// http://gage14.upc.es/gLAB/HTML/LaunchHTML.html
// http://gage14.upc.es/gLAB/HTML/Observation_Rinex_v2.11.html
func parseVersionType(line string) (version float64, fileType, satellite, label string, err error) {
	label = line[60:80]
	if label != "RINEX VERSION / TYPE" {
		err = errors.New("Unknown header label")
		return
	}
	version, err = strconv.ParseFloat(strings.TrimSpace(line[0:9]), 64)
	if err != nil {
		return
	}
	fileType = line[20:21]
	satellite = line[40:41]
	return
}
func main() {
	line := "     2.11           OBSERVATION DATA    G (GPS)             RINEX VERSION / TYPE\n"
	version, fileType, satellite, label, err := parseVersionType(line)
	fmt.Printf("%g %q %q %q %v\n", version, fileType, satellite, label, err)
}
Output:
2.11 "O" "G" "RINEX VERSION / TYPE" <nil>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论