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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法