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.

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

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改