dpoh61610 2017-01-02 21:20 采纳率: 100%
浏览 44
已采纳

从Go中具有可变行尾的文件中读取行

How can I read lines from a file where the line endings are carriage return (CR), newline (NL), or both?

The PDF specification allows lines to end with CR, LF, or CRLF.

  • bufio.Reader.ReadString() and bufio.Reader.ReadBytes() allow a single delimiter byte.

  • bufio.Scanner.Scan() handles optionally preceded by , but not a lone .

    The end-of-line marker is one optional carriage return followed by one mandatory newline.

Do I need to write my own function that uses bufio.Reader.ReadByte()?

  • 写回答

1条回答 默认 最新

  • dongtangyi8962 2017-01-02 21:48
    关注

    You can write custom bufio.SplitFunc for bufio.Scanner. E.g:

    // Mostly bufio.ScanLines code:
    func ScanPDFLines(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 {
            if data[i] == '
    ' {
                // We have a line terminated by single newline.
                return i + 1, data[0:i], nil
            }
            advance = i + 1
            if len(data) > i+1 && data[i+1] == '
    ' {
                advance += 1
            }
            return advance, data[0:i], nil
        }
        // If we're at EOF, we have a final, non-terminated line. Return it.
        if atEOF {
            return len(data), data, nil
        }
        // Request more data.
        return 0, nil, nil
    }
    

    And use it like:

    scan := bufio.NewScanner(r)
    scan.Split(ScanPDFLines)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输