Go 中的文本处理 - 如何将字符串转换为字节?
Text processing in Go - how to convert string to byte?
我正在写一个小程序来给段落编号:
- 以[1]...,[2]....
的形式在每段前加上段号
- 应排除文章标题。
这是我的程序:
package main
import (
"fmt"
"io/ioutil"
)
var s_end = [3]string{".", "!", "?"}
func main() {
b, err := ioutil.ReadFile("i_have_a_dream.txt")
if err != nil {
panic(err)
}
p_num, s_num := 1, 1
for _, char := range b {
fmt.Printf("[%s]", p_num)
p_num += 1
if char == byte("\n") {
fmt.Printf("\n[%s]", p_num)
p_num += 1
} else {
fmt.Printf(char)
}
}
}
http://play.golang.org/p/f4S3vQbglY
我收到这个错误:
prog.go:21: cannot convert "\n" to type byte
prog.go:21: cannot convert "\n" (type string) to type byte
prog.go:21: invalid operation: char == "\n" (mismatched types byte and string)
prog.go:25: cannot use char (type byte) as type string in argument to fmt.Printf
[process exited with non-zero status]
如何将字符串转换为字节?
处理文本的一般做法是什么?读入,按字节解析,还是按行解析?
更新
我通过将缓冲区字节转换为字符串,用正则表达式替换字符串解决了这个问题。 (感谢@Tomasz Kłak 的正则表达式帮助)
我把代码放在这里供参考
package main
import (
"fmt"
"io/ioutil"
"regexp"
)
func main() {
b, err := ioutil.ReadFile("i_have_a_dream.txt")
if err != nil {
panic(err)
}
s := string(b)
r := regexp.MustCompile("(\r\n)+")
counter := 1
repl := func(match string) string {
p_num := counter
counter++
return fmt.Sprintf("%s [%d] ", match, p_num)
}
fmt.Println(r.ReplaceAllStringFunc(s, repl))
}
使用 "\n"
将其视为数组,使用 '\n'
将其视为单个字符。
A string
无法以有意义的方式转换为 byte
。使用以下方法之一:
- 如果您有像
"a"
这样的字符串文字,请考虑使用像 'a'
这样的 rune literal,它可以转换为 byte
.
- 如果您想从
string
中取出 byte
,请使用 index expression,例如 myString[42]
。
- 如果要将
string
的内容解释为(十进制)数字,请使用 strconv.Atoi()
or strconv.ParseInt()
。
请注意,在 Go 中习惯于编写可以处理 Unicode 字符的程序。解释如何做到这一点对于这个答案来说太多了,但是那里有教程解释了需要注意的事情。
我正在写一个小程序来给段落编号:
- 以[1]...,[2].... 的形式在每段前加上段号
- 应排除文章标题。
这是我的程序:
package main
import (
"fmt"
"io/ioutil"
)
var s_end = [3]string{".", "!", "?"}
func main() {
b, err := ioutil.ReadFile("i_have_a_dream.txt")
if err != nil {
panic(err)
}
p_num, s_num := 1, 1
for _, char := range b {
fmt.Printf("[%s]", p_num)
p_num += 1
if char == byte("\n") {
fmt.Printf("\n[%s]", p_num)
p_num += 1
} else {
fmt.Printf(char)
}
}
}
http://play.golang.org/p/f4S3vQbglY
我收到这个错误:
prog.go:21: cannot convert "\n" to type byte
prog.go:21: cannot convert "\n" (type string) to type byte
prog.go:21: invalid operation: char == "\n" (mismatched types byte and string)
prog.go:25: cannot use char (type byte) as type string in argument to fmt.Printf
[process exited with non-zero status]
如何将字符串转换为字节?
处理文本的一般做法是什么?读入,按字节解析,还是按行解析?
更新
我通过将缓冲区字节转换为字符串,用正则表达式替换字符串解决了这个问题。 (感谢@Tomasz Kłak 的正则表达式帮助)
我把代码放在这里供参考
package main
import (
"fmt"
"io/ioutil"
"regexp"
)
func main() {
b, err := ioutil.ReadFile("i_have_a_dream.txt")
if err != nil {
panic(err)
}
s := string(b)
r := regexp.MustCompile("(\r\n)+")
counter := 1
repl := func(match string) string {
p_num := counter
counter++
return fmt.Sprintf("%s [%d] ", match, p_num)
}
fmt.Println(r.ReplaceAllStringFunc(s, repl))
}
使用 "\n"
将其视为数组,使用 '\n'
将其视为单个字符。
A string
无法以有意义的方式转换为 byte
。使用以下方法之一:
- 如果您有像
"a"
这样的字符串文字,请考虑使用像'a'
这样的 rune literal,它可以转换为byte
. - 如果您想从
string
中取出byte
,请使用 index expression,例如myString[42]
。 - 如果要将
string
的内容解释为(十进制)数字,请使用strconv.Atoi()
orstrconv.ParseInt()
。
请注意,在 Go 中习惯于编写可以处理 Unicode 字符的程序。解释如何做到这一点对于这个答案来说太多了,但是那里有教程解释了需要注意的事情。