dsgd4654674 2016-12-20 09:29 采纳率: 0%
浏览 277
已采纳

strconv.ParseInt给出0

I'm trying to parse a file of this forme:

12
0,1,2,3,1,2,3,4,2,3,4,5
1,0,1,2,2,1,2,3,3,2,3,4
2,1,0,1,3,2,1,2,4,3,2,3
3,2,1,0,4,3,2,1,5,4,3,2
1,2,3,4,0,1,2,3,1,2,3,4
2,1,2,3,1,0,1,2,2,1,2,3
3,2,1,2,2,1,0,1,3,2,1,2
4,3,2,1,3,2,1,0,4,3,2,1
2,3,4,5,1,2,3,4,0,1,2,3
3,2,3,4,2,1,2,3,1,0,1,2
4,3,2,3,3,2,1,2,2,1,0,1
5,4,3,2,4,3,2,1,3,2,1,0
0,5,2,4,1,0,0,6,2,1,1,1
5,0,3,0,2,2,2,0,4,5,0,0
2,3,0,0,0,0,0,5,5,2,2,2
4,0,0,0,5,2,2,10,0,0,5,5
1,2,0,5,0,10,0,0,0,5,1,1
0,2,0,2,10,0,5,1,1,5,4,0
0,2,0,2,0,5,0,10,5,2,3,3
6,0,5,10,0,1,10,0,0,0,5,0
2,4,5,0,0,1,5,0,0,0,10,10
1,5,2,0,5,5,2,0,0,0,5,0
1,0,2,5,1,4,3,5,10,5,0,2
1,0,2,5,1,0,3,0,10,0,2,0

Where the first line give us the size of the matrix: n x n The n-following lines are the matrix D. Then the n-following lines are the matrix W. So there is 2n + 1 lines.

Here is the code to parse it and put it in variables.

func readFile(path string) (int64, Matrix, Matrix) {
    // open the file
    f, _ := os.Open(path)
    defer f.Close()

    // init the new reader on the opened file
    r := bufio.NewReader(f)

    // we get the n value
    line, _ := r.ReadString('
')
    splitedLine := strings.Fields(line)
    tmp, _ := strconv.ParseInt(splitedLine[0], 10, 64)
    n := int64(tmp)

    // we init the matrix W and D
    D := Matrix{}
    D.matrix = make([][]int64, n)
    for i, _ := range D.matrix {
        D.matrix[i] = make([]int64, n)
    }

    W := Matrix{}
    W.matrix = make([][]int64, n)
    for i, _ := range W.matrix {
        W.matrix[i] = make([]int64, n)
    }

    // loop on the n first element
    iter := int64(0)
    for iter < n {
        // we get the n following elements
        line, _ = r.ReadString('
')
        for index, ele := range strings.Split(line, ",") {
            D.matrix[iter][index], _ = strconv.ParseInt(ele, 10, 64)
        }
        iter++
    }

    iter = 0
    for iter < n {
        // we get the n following elements
        line, _ = r.ReadString('
')
        for index, ele := range strings.Split(line, ",") {
            W.matrix[iter][index], _ = strconv.ParseInt(ele, 10, 64)
        }
        iter++
    }

    return n, W, D

}

The resultat for W should be like:

[ 0.,  1.,  2.,  3.,  1.,  2.,  3.,  4.,  2.,  3.,  4.,  5.],
[ 1.,  0.,  1.,  2.,  2.,  1.,  2.,  3.,  3.,  2.,  3.,  4.],
[ 2.,  1.,  0.,  1.,  3.,  2.,  1.,  2.,  4.,  3.,  2.,  3.],
[ 3.,  2.,  1.,  0.,  4.,  3.,  2.,  1.,  5.,  4.,  3.,  2.],
[ 1.,  2.,  3.,  4.,  0.,  1.,  2.,  3.,  1.,  2.,  3.,  4.],
[ 2.,  1.,  2.,  3.,  1.,  0.,  1.,  2.,  2.,  1.,  2.,  3.],
[ 3.,  2.,  1.,  2.,  2.,  1.,  0.,  1.,  3.,  2.,  1.,  2.],
[ 4.,  3.,  2.,  1.,  3.,  2.,  1.,  0.,  4.,  3.,  2.,  1.],
[ 2.,  3.,  4.,  5.,  1.,  2.,  3.,  4.,  0.,  1.,  2.,  3.],
[ 3.,  2.,  3.,  4.,  2.,  1.,  2.,  3.,  1.,  0.,  1.,  2.],
[ 4.,  3.,  2.,  3.,  3.,  2.,  1.,  2.,  2.,  1.,  0.,  1.],
[ 5.,  4.,  3.,  2.,  4.,  3.,  2.,  1.,  3.,  2.,  1.,  0.]

But when I print it; it gives me :

[0 1 2 3 1 2 3 4 2 3 4 0]
[1 0 1 2 2 1 2 3 3 2 3 0]
[2 1 0 1 3 2 1 2 4 3 2 0]
[3 2 1 0 4 3 2 1 5 4 3 0]
[1 2 3 4 0 1 2 3 1 2 3 0]
[2 1 2 3 1 0 1 2 2 1 2 0]
[3 2 1 2 2 1 0 1 3 2 1 0]
[4 3 2 1 3 2 1 0 4 3 2 0]
[2 3 4 5 1 2 3 4 0 1 2 0]
[3 2 3 4 2 1 2 3 1 0 1 0]
[4 3 2 3 3 2 1 2 2 1 0 0]
[5 4 3 2 4 3 2 1 3 2 1 0]

I don't know why. Thanks for your reply.

  • 写回答

1条回答 默认 最新

  • douhuo1738 2016-12-20 09:43
    关注

    Know that Reader.ReadString() returns you a string value with the delimieter. So r.ReadString(' ') will return you a string with the trailing newline character.

    And when you split the string at the commas, the last part will contain the newline character, and attempting to parse the number from it will fail, as shown in this example:

    line := "0,1,2
    "
    for _, ele := range strings.Split(line, ",") {
        fmt.Println(strconv.ParseInt(ele, 10, 64))
    }
    

    Output (try it on the Go Playground):

    0 <nil>
    1 <nil>
    0 strconv.ParseInt: parsing "2
    ": invalid syntax
    

    You religiously omit error checks, don't do that! As you can see in the above example, checking and printing the error returned by strconv.ParseInt() immediately revealed the error and what's going wrong.

    What you may do is cut the trailing newline character before attempting to parse any number from it, e.g.:

    strconv.ParseInt(strings.TrimSpace(ele), 10, 64)
    

    Or use the bufio.Scanner which can scan lines and it cuts trailing newlines automatically.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CSS实现渐隐虚线边框
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题