Golang: Skipping Whitespace in a file

huangapple go评论72阅读模式
英文:

Golang: Skipping Whitespace in a file

问题

在Go语言中读取文件时,我试图跳过所有的空白字符,但是我找不到正确的方法来实现。希望能得到帮助。

file, err := os.Open(filename) // 用于读取文件
if err != nil {
    log.Fatal(err)
}

// 跳过空白字符
c := make([]byte, 1)
char, err := file.Read(c)

for {
    // 捕获意外错误
    if err != nil && err != io.EOF {
        panic(err)
    }
    if err == io.EOF || !unicode.IsSpace(int(c)) {
        break
    }
    // 获取下一个字符
    char, err = file.Read(c)
}

我只是简单地尝试创建一个文件的扫描器,以便逐个字符地读取并忽略空白字符。

编辑

我改变了一些东西,使用了bufio.Reader,但是仍然遇到了问题。正确的方法是如何逐个字符地读取文件,以便可以与特定符号(如'A')进行比较,同时可以忽略空白字符(如unicode.IsSpace(rune))?

char, size, err := reader.ReadRune()

// 跳过空白字符和注释
for {
    // 捕获意外错误
    if err != nil && err != io.EOF {
        panic(err)
    }
    if err == io.EOF {
        break
    }

    // 当没有数据或者是空格时跳过
    if size != 0 && char == '{' {
        // 忽略注释
        // 文档规定没有嵌套注释
        for char != '}' {
            char, size, err = reader.ReadRune()
        }
    } else if !unicode.IsSpace(char) {
        break
    }

    // 处理字节
    fmt.Print(char)

    // 获取下一个字符
    char, size, err = reader.ReadRune()
}

希望对你有帮助!

英文:

In reading a file in Go I am attempting to skip all of the white spaces; however, I am having issues finding the correct method to do this. Any assistance would be appreciated

file, err := os.Open(filename) // For read access.
        this.file = file
        if err != nil {
                log.Fatal(err)
        }
//skip white space

  c := make([]byte, 1)
    
    char, err := this.file.Read(c)
    
    //skip white space
    for {
            //catch unintended errors
            if err != nil && err != io.EOF {
                    panic(err)
            }
            if err == io.EOF || !unicode.IsSpace(int(c)) {
                    break
            }
            //get next
            char, err := this.file.Read(c)
    }

I am just simply attempting to create a scanner for a file to read a single character at a time and ignore whitespace

EDIT

I changed a few things around to make use of bufio.Reader; however I have still fallen into issue What is the correct way to read a file character by character so that it might be compared to a specific symbol such as 'A' but also can ignore whitespace i.e unicode.isSpace(rune)

char, size, err := this.reader.ReadRune()
    //skip white space and comments
    for {
            //catch unintended errors
            if err != nil && err != io.EOF {
                    panic(err)
            }
            if err == io.EOF {
                    break
            }

            //skip it when their is no data or a space
            if size != 0 && char == '{' {
                    //Ignore Comments
                    //Documentation specifies no nested comments
                    for char != '}' {
                            char, size, err = this.reader.ReadRune()
                    }
            } else if !unicode.IsSpace(char) {
                    break
            }

            // Do something with the byte
            fmt.Print(char)

            //get next
            char, size, err = this.reader.ReadRune()
    }

答案1

得分: 2

除非我误解了你的问题,否则似乎你在遇到空格时想要使用continue语句。

c := make([]byte, 100)

n, err := this.file.Read(c)

//跳过空格
for {
	//捕获意外错误
	if err != nil && err != io.EOF {
		panic(err)
	}
	if err == io.EOF {
		break
	}

    for i := 0; i < n; i++ {
        ch := c[i]

        switch ch {
        case '{': // 做某事
        case '}': // 做其他事情
        default:
            if unicode.IsSpace(int(ch)) {
                continue
            }
            // 做其他操作
        }
    }

	//获取下一个
    n, err = this.file.Read(c)
}

我不知道为什么你一次读取一个字节,但我保留了这种方式,以防这是有意为之的。至少,我认为你应该读取完整的Unicode字符而不是单个字节。

英文:

Unless I'm misunderstanding your question, it would seem that you'd want a continue statement when encountering a space.

c := make([]byte, 100)

n, err := this.file.Read(c)

//skip white space
for {
	//catch unintended errors
	if err != nil &amp;&amp; err != io.EOF {
		panic(err)
	}
	if err == io.EOF {
		break
	}

    for i := 0; i &lt; n; i++ {
        ch := c[i]

        switch ch {
        case &#39;{&#39;: // Do something
        case &#39;}&#39;: // Do something else
        default:
            if unicode.IsSpace(int(ch)) {
                continue
            }
            // Do whatever
        }
    }

	    //get next
    n, err = this.file.Read(c)
}

I don't know why you're reading one byte at a time, but I left it that way in case it's intentional. At the very least, I'd think you'd want to read full unicode characters instead of individual bytes.

huangapple
  • 本文由 发表于 2015年9月25日 03:28:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/32769420.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定