douxiangui5011 2015-12-23 12:00
浏览 220
已采纳

Golang:读取包含多行的文本文件

I have a text file with multi-line rows, delimited by a blank line. What would be the best way to read that row for row in Go?

I think I may have to use a Scanner with my own Split function, but just wondering if there is a better/easier way that I am missing.

I have tried using my own Splitfunc based on bufio.ScanLines:

func MyScanLines(data []byte, atEOF bool) (advance int, token []byte,    err error) {
    if atEOF && len(data) == 0 {
            return 0, nil, nil
    }
    if i := bytes.IndexAny(data, "

"); i >= 0 {
            return i + 1, dropCR(data[0:i]), nil
    }
    if atEOF {
            return len(data), dropCR(data), nil
    }
    return 0, nil, nil
}

But I get an error on the IndexAny call: "syntax error: unexpected semicolon or newline, expecting )" - Fixed that

Update: Fixed the syntax error above as suggested, but I only get the first line returned. I am reading the file as follows:

scanner.Split(MyScanLines)
scanner.Scan()
fmt.Println(scanner.Text())

Any suggestions?

Example of test file I am trying to read:

Name = "John"
Surname = "Smith"
Val1 = 700
Val2 = 800

Name = "Pete"
Surname = "Jones"
Val1 = 555
Val2 = 666
Val3 = 444

 .
 .
 .
  • 写回答

4条回答 默认 最新

  • dqoag62688 2015-12-24 23:21
    关注

    Broken out. First understand scanning and make sure that is working:

    package main
    
    import (
        "bufio"
        "fmt"
        "strings"
    )
    
    func main() {
        scanner := bufio.NewScanner(strings.NewReader(data))
        for scanner.Scan() {
            l := scanner.Text()
            fmt.Println(l)
    
        }
    
    }
    
    var data = `
    Name = "John"
    Surname = "Smith"
    Val1 = 700
    Val2 = 800
    
    Name = "Pete"
    Surname = "Jones"
    Val1 = 555
    Val2 = 666
    Val3 = 444
    `
    

    Here is the code on the Go playground.

    Next, gather the data you need into a slice. There is probably a way to check end of file, EOF, but I wasn't able to find it. This is what I came up with and this works:

    package main
    
    import (
        "bufio"
        "fmt"
        "strings"
    )
    
    func main() {
        buffer := [][]string{}
        block := []string{}
        scanner := bufio.NewScanner(strings.NewReader(data))
        for scanner.Scan() {
            l := scanner.Text()
    
            if len(l) != 0 {
                block = append(block, l)
                continue
            }
    
            if len(l) == 0 && len(block) != 0 {
                buffer = append(buffer, block)
                block = []string{}
                continue
            }
    
            if len(l) == 0 {
                block = []string{}
                continue
            }
    
        }
    
        if len(block) != 0 {
            buffer = append(buffer, block)
            block = []string{}
        }
    
        fmt.Println("PRINTING BUFFER - END OF PROGRAM - ALL DATA PROCESSED:", buffer)
    
    }
    
    var data = `
    Name = "John"
    Surname = "Smith"
    Val1 = 700
    Val2 = 800
    
    Name = "Pete"
    Surname = "Jones"
    Val1 = 555
    Val2 = 666
    Val3 = 444
    `
    

    Here is the code on the playground.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP