douri4459 2014-07-31 22:59
浏览 80
已采纳

在Golang中解析格式化的字符串

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 + " ". 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
"
    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条回答 默认 最新

  • duanboxue3422 2014-08-01 01:10
    关注

    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
    "
        version, fileType, satellite, label, err := parseVersionType(line)
        fmt.Printf("%g %q %q %q %v
    ", version, fileType, satellite, label, err)
    }
    

    Output:

    2.11 "O" "G" "RINEX VERSION / TYPE" <nil>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,