在Golang中解析格式化字符串

huangapple go评论118阅读模式
英文:

Parse formatted string in Golang

问题

我正在尝试使用Golang解析GNSS RINEX文件。

例如,这是VERSION行的RINEX规范:

  1. +--------------------+------------------------------------------+------------+
  2. |RINEX VERSION / TYPE| - 格式版本 (2.11) | F9.2,11X, |
  3. | | - 文件类型 ('O' 表示观测数据) | A1,19X, |
  4. | | - 卫星系统: 空白或 'G': GPS | A1,19X |
  5. | | 'R': GLONASS | |
  6. | | 'S': Geostationary | |
  7. | | signal payload | |
  8. | | 'E': Galileo | |
  9. | | 'M': Mixed | |
  10. +--------------------+------------------------------------------+------------+

RINEX文件中的每一行都有固定的宽度为80个ASCII字符 + "\n"。在这个例子中,前9个字符表示版本号(浮点数)。

在Python中,我可以使用:

  1. struct.unpack("9s11s1s19s1s19s20s", line)

这将返回一个包含7个字符串的元组。

我是Go的新手,一直在尝试使用fmt.Sscanf来读取格式化文本:

  1. func main() {
  2. str := " 2.11 OBSERVATION DATA G (GPS) RINEX VERSION / TYPE\n"
  3. var value float32
  4. a, err := fmt.Sscanf(str,"%9.2f", &value)
  5. fmt.Println(a)
  6. fmt.Println(err)
  7. fmt.Println(value)
  8. }

返回:

  1. 0
  2. bad verb %. for float32
  3. 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:

  1. +--------------------+------------------------------------------+------------+
  2. |RINEX VERSION / TYPE| - Format version (2.11) | F9.2,11X, |
  3. | | - File type ('O' for Observation Data) | A1,19X, |
  4. | | - Satellite System: blank or 'G': GPS | A1,19X |
  5. | | 'R': GLONASS | |
  6. | | 'S': Geostationary | |
  7. | | signal payload | |
  8. | | 'E': Galileo | |
  9. | | 'M': Mixed | |
  10. +--------------------+------------------------------------------+------------+

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:

  1. 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:

  1. func main() {
  2. str := " 2.11 OBSERVATION DATA G (GPS) RINEX VERSION / TYPE\n"
  3. var value float32
  4. a, err := fmt.Sscanf(str,"%9.2f", &value)
  5. fmt.Println(a)
  6. fmt.Println(err)
  7. fmt.Println(value)
  8. }

returns:

  1. 0
  2. bad verb %. for float32
  3. 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

例如,

  1. package main
  2. import (
  3. "errors"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. )
  8. // http://gage14.upc.es/gLAB/HTML/LaunchHTML.html
  9. // http://gage14.upc.es/gLAB/HTML/Observation_Rinex_v2.11.html
  10. func parseVersionType(line string) (version float64, fileType, satellite, label string, err error) {
  11. label = line[60:80]
  12. if label != "RINEX VERSION / TYPE" {
  13. err = errors.New("未知的头标签")
  14. return
  15. }
  16. version, err = strconv.ParseFloat(strings.TrimSpace(line[0:9]), 64)
  17. if err != nil {
  18. return
  19. }
  20. fileType = line[20:21]
  21. satellite = line[40:41]
  22. return
  23. }
  24. func main() {
  25. line := " 2.11 OBSERVATION DATA G (GPS) RINEX VERSION / TYPE\n"
  26. version, fileType, satellite, label, err := parseVersionType(line)
  27. fmt.Printf("%g %q %q %q %v\n", version, fileType, satellite, label, err)
  28. }

输出:

  1. 2.11 "O" "G" "RINEX VERSION / TYPE" <nil>
英文:

For example,

  1. package main
  2. import (
  3. &quot;errors&quot;
  4. &quot;fmt&quot;
  5. &quot;strconv&quot;
  6. &quot;strings&quot;
  7. )
  8. // http://gage14.upc.es/gLAB/HTML/LaunchHTML.html
  9. // http://gage14.upc.es/gLAB/HTML/Observation_Rinex_v2.11.html
  10. func parseVersionType(line string) (version float64, fileType, satellite, label string, err error) {
  11. label = line[60:80]
  12. if label != &quot;RINEX VERSION / TYPE&quot; {
  13. err = errors.New(&quot;Unknown header label&quot;)
  14. return
  15. }
  16. version, err = strconv.ParseFloat(strings.TrimSpace(line[0:9]), 64)
  17. if err != nil {
  18. return
  19. }
  20. fileType = line[20:21]
  21. satellite = line[40:41]
  22. return
  23. }
  24. func main() {
  25. line := &quot; 2.11 OBSERVATION DATA G (GPS) RINEX VERSION / TYPE\n&quot;
  26. version, fileType, satellite, label, err := parseVersionType(line)
  27. fmt.Printf(&quot;%g %q %q %q %v\n&quot;, version, fileType, satellite, label, err)
  28. }

Output:

  1. 2.11 &quot;O&quot; &quot;G&quot; &quot;RINEX VERSION / TYPE&quot; &lt;nil&gt;

huangapple
  • 本文由 发表于 2014年8月1日 06:59:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/25070312.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定