英文:
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 && err != io.EOF {
panic(err)
}
if err == io.EOF {
break
}
for i := 0; i < n; i++ {
ch := c[i]
switch ch {
case '{': // Do something
case '}': // 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论