读取一个文本文件,替换其中的单词,并将结果输出到另一个文本文件中。

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

Read a text file, replace its words, output to another text file

问题

所以我正在尝试用GO编写一个程序,用于将一个充满代码的文本文件转换为GO代码,然后将该文件保存为GO文件或文本文件。我一直在努力弄清楚如何保存我对文本文件所做的更改,但是我唯一能看到更改的方法是通过println语句,因为我使用strings.replace来搜索存储在字符串数组中的文本文件并更改需要更改的每个单词的出现(例如,BEGIN -> {和END -> })。那么,是否有其他我不知道的在GO中搜索和替换的方法,或者有没有一种我不知道的编辑文本文件的方法,或者这是不可能的?

谢谢

以下是我目前的代码。

  1. package main
  2. import (
  3. "os"
  4. "bufio"
  5. "bytes"
  6. "io"
  7. "fmt"
  8. "strings"
  9. )
  10. func readLines(path string) (lines []string, errr error) {
  11. var (
  12. file *os.File
  13. part []byte
  14. prefix bool
  15. )
  16. if file, errr = os.Open(path); errr != nil {
  17. return
  18. }
  19. defer file.Close()
  20. reader := bufio.NewReader(file)
  21. buffer := bytes.NewBuffer(make([]byte, 0))
  22. for {
  23. if part, prefix, errr = reader.ReadLine(); errr != nil {
  24. break
  25. }
  26. buffer.Write(part)
  27. if !prefix {
  28. lines = append(lines, buffer.String())
  29. buffer.Reset()
  30. }
  31. }
  32. if errr == io.EOF {
  33. errr = nil
  34. }
  35. return
  36. }
  37. func writeLines(lines []string, path string) (errr error) {
  38. var (
  39. file *os.File
  40. )
  41. if file, errr = os.Create(path); errr != nil {
  42. return
  43. }
  44. defer file.Close()
  45. for _,item := range lines {
  46. _, errr := file.WriteString(strings.TrimSpace(item) + "\n");
  47. if errr != nil {
  48. fmt.Println(errr)
  49. break
  50. }
  51. }
  52. return
  53. }
  54. func FixBegin(lines []string) (errr error) {
  55. var(
  56. a string
  57. )
  58. for i := 0; ; i++ {
  59. a = lines[i];
  60. fmt.Println(strings.Replace(a, "BEGIN", "{", -1))
  61. }
  62. return
  63. }
  64. func FixEnd(lines []string) (errr error) {
  65. var(
  66. a string
  67. )
  68. for i := 0; ; i++ {
  69. a = lines[i];
  70. fmt.Println(strings.Replace(a, "END", "}", -1))
  71. }
  72. return
  73. }
  74. func main() {
  75. lines, errr := readLines("foo.txt")
  76. if errr != nil {
  77. fmt.Println("Error: %s\n", errr)
  78. return
  79. }
  80. for _, line := range lines {
  81. fmt.Println(line)
  82. }
  83. errr = FixBegin(lines)
  84. errr = writeLines(lines, "beer2.txt")
  85. fmt.Println(errr)
  86. errr = FixEnd(lines)
  87. lines, errr = readLines("beer2.txt")
  88. if errr != nil {
  89. fmt.Println("Error: %s\n", errr)
  90. return
  91. }
  92. errr = writeLines(lines, "beer2.txt")
  93. fmt.Println(errr)
  94. }
英文:

So I am trying to make a program in GO to take a text file full of code and convert that into GO code and then save that file into a GO file or text file. I have been trying to figure out how to save the changes I made to the text file, but the only way I can see the changes is through a println statement because I am using strings.replace to search the string array that the text file is stored in and change each occurrence of a word that needs to be changed (ex. BEGIN -> { and END -> }). So is there any other way of searching and replacing in GO I don't know about or is there a way to edit a text file that I don't know about or is this impossible?

Thanks

Here is the code I have so far.

  1. package main
  2. import (
  3. "os"
  4. "bufio"
  5. "bytes"
  6. "io"
  7. "fmt"
  8. "strings"
  9. )
  10. func readLines(path string) (lines []string, errr error) {
  11. var (
  12. file *os.File
  13. part []byte
  14. prefix bool
  15. )
  16. if file, errr = os.Open(path); errr != nil {
  17. return
  18. }
  19. defer file.Close()
  20. reader := bufio.NewReader(file)
  21. buffer := bytes.NewBuffer(make([]byte, 0))
  22. for {
  23. if part, prefix, errr = reader.ReadLine(); errr != nil {
  24. break
  25. }
  26. buffer.Write(part)
  27. if !prefix {
  28. lines = append(lines, buffer.String())
  29. buffer.Reset()
  30. }
  31. }
  32. if errr == io.EOF {
  33. errr = nil
  34. }
  35. return
  36. }
  37. func writeLines(lines []string, path string) (errr error) {
  38. var (
  39. file *os.File
  40. )
  41. if file, errr = os.Create(path); errr != nil {
  42. return
  43. }
  44. defer file.Close()
  45. for _,item := range lines {
  46. _, errr := file.WriteString(strings.TrimSpace(item) + "\n");
  47. if errr != nil {
  48. fmt.Println(errr)
  49. break
  50. }
  51. }
  52. return
  53. }
  54. func FixBegin(lines []string) (errr error) {
  55. var(
  56. a string
  57. )
  58. for i := 0; ; i++ {
  59. a = lines[i];
  60. fmt.Println(strings.Replace(a, "BEGIN", "{", -1))
  61. }
  62. return
  63. }
  64. func FixEnd(lines []string) (errr error) {
  65. var(
  66. a string
  67. )
  68. for i := 0; ; i++ {
  69. a = lines[i];
  70. fmt.Println(strings.Replace(a, "END", "}", -1))
  71. }
  72. return
  73. }
  74. func main() {
  75. lines, errr := readLines("foo.txt")
  76. if errr != nil {
  77. fmt.Println("Error: %s\n", errr)
  78. return
  79. }
  80. for _, line := range lines {
  81. fmt.Println(line)
  82. }
  83. errr = FixBegin(lines)
  84. errr = writeLines(lines, "beer2.txt")
  85. fmt.Println(errr)
  86. errr = FixEnd(lines)
  87. lines, errr = readLines("beer2.txt")
  88. if errr != nil {
  89. fmt.Println("Error: %s\n", errr)
  90. return
  91. }
  92. errr = writeLines(lines, "beer2.txt")
  93. fmt.Println(errr)
  94. }

答案1

得分: 3

jnml@fsc-r630:~/src/tmp/SO/13789882$ ls
foo.txt main.go
jnml@fsc-r630:~/src/tmp/SO/13789882$ cat main.go
package main

import (
"bytes"
"io/ioutil"
"log"
)

func main() {
src, err := ioutil.ReadFile("foo.txt")
if err != nil {
log.Fatal(err)
}

  1. src = bytes.Replace(src, []byte("BEGIN"), []byte("{"), -1)
  2. src = bytes.Replace(src, []byte("END"), []byte("}"), -1)
  3. if err = ioutil.WriteFile("beer2.txt", src, 0666); err != nil {
  4. log.Fatal(err)
  5. }

}
jnml@fsc-r630:~/src/tmp/SO/13789882$ cat foo.txt
BEGIN
FILE F(KIND=REMOTE);
EBCDIC ARRAY E[0:11];
REPLACE E BY "HELLO WORLD!";
WRITE(F, *, E);
END.
jnml@fsc-r630:~/src/tmp/SO/13789882$ go run main.go
jnml@fsc-r630:~/src/tmp/SO/13789882$ cat beer2.txt
{
FILE F(KIND=REMOTE);
EBCDIC ARRAY E[0:11];
REPLACE E BY "HELLO WORLD!";
WRITE(F, *, E);
}.

英文:
  1. jnml@fsc-r630:~/src/tmp/SO/13789882$ ls
  2. foo.txt main.go
  3. jnml@fsc-r630:~/src/tmp/SO/13789882$ cat main.go
  4. package main
  5. import (
  6. "bytes"
  7. "io/ioutil"
  8. "log"
  9. )
  10. func main() {
  11. src, err := ioutil.ReadFile("foo.txt")
  12. if err != nil {
  13. log.Fatal(err)
  14. }
  15. src = bytes.Replace(src, []byte("BEGIN"), []byte("{"), -1)
  16. src = bytes.Replace(src, []byte("END"), []byte("}"), -1)
  17. if err = ioutil.WriteFile("beer2.txt", src, 0666); err != nil {
  18. log.Fatal(err)
  19. }
  20. }
  21. jnml@fsc-r630:~/src/tmp/SO/13789882$ cat foo.txt
  22. BEGIN
  23. FILE F(KIND=REMOTE);
  24. EBCDIC ARRAY E[0:11];
  25. REPLACE E BY "HELLO WORLD!";
  26. WRITE(F, *, E);
  27. END.
  28. jnml@fsc-r630:~/src/tmp/SO/13789882$ go run main.go
  29. jnml@fsc-r630:~/src/tmp/SO/13789882$ cat beer2.txt
  30. {
  31. FILE F(KIND=REMOTE);
  32. EBCDIC ARRAY E[0:11];
  33. REPLACE E BY "HELLO WORLD!";
  34. WRITE(F, *, E);
  35. }.
  36. jnml@fsc-r630:~/src/tmp/SO/13789882$

答案2

得分: 3

我同意@jnml关于使用ioutil来读取文件和写回文件的建议。但是我认为替换不应该通过多次对[]byte的操作来完成。代码和数据都是字符串/文本,应该以此方式处理(即使处理非ASCII/UTF8编码需要额外的工作);一次性替换(同时替换所有占位符)可以避免替换之前更改的结果的风险(即使我的正则表达式提案必须改进以处理非平凡的任务)。

  1. package main
  2. import(
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "regexp"
  7. "strings"
  8. )
  9. func main() {
  10. // (1) 读取文件
  11. data, err := ioutil.ReadFile("../tmpl/xpl.go")
  12. if err != nil {
  13. log.Fatal("ioutil.ReadFile: ", err)
  14. }
  15. s := string(data)
  16. fmt.Printf("----\n%s----\n", s)
  17. // => 适用于已知其他编码(非ASCII或UTF8)的文件的函数
  18. // (2) 创建一个将要替换的占位符到替换值的映射
  19. x := map[string]string {
  20. "BEGIN" : "{",
  21. "END" : "}"}
  22. ks := make([]string, 0, len(x))
  23. for k := range x {
  24. ks = append(ks, k)
  25. }
  26. // => 从映射中获取键的函数
  27. // (3) 创建一个正则表达式,用于查找将要替换的占位符
  28. p := strings.Join(ks, "|")
  29. fmt.Printf("/%s/\n", p)
  30. r := regexp.MustCompile(p)
  31. // => 需要更多考虑的有趣字符和顺序
  32. // (4) 创建一个回调函数,用于..ReplaceAllStringFunc,该函数了解映射x
  33. f := func(s string) string {
  34. fmt.Printf("*** '%s'\n", s)
  35. return x
    展开收缩
  36. }
  37. // => 以可重用的方式执行步骤(2)..(4)的函数?
  38. // (5) 进行替换(s将被覆盖为结果)
  39. s = r.ReplaceAllStringFunc(s, f)
  40. fmt.Printf("----\n%s----\n", s)
  41. // (6) 写回文件
  42. err = ioutil.WriteFile("result.go", []byte(s), 0644)
  43. if err != nil {
  44. log.Fatal("ioutil.WriteFile: ", err)
  45. }
  46. // => 适用于已知其他编码(非ASCII或UTF8)的文件的函数
  47. }

输出:

  1. go run 13789882.go
  2. ----
  3. func main() BEGIN
  4. END
  5. ----
  6. /BEGIN|END/
  7. *** 'BEGIN'
  8. *** 'END'
  9. ----
  10. func main() {
  11. }
  12. ----
英文:

I agree with @jnml wrt using ioutil to slurp the file and to write it back. But I think that the replacing shouldn't be done by multiple passes over []byte. Code and data are strings/text and should be treated as such (even if dealing with non ascii/utf8 encodings requires estra work); a one pass replacement (of all placeholders 'at once') avoids the risk of replacing results of previous changes (even if my regexp proposal must be improved to handle non-trivial tasks).

  1. package main
  2. import(
  3. "fmt"
  4. "io/ioutil"
  5. "log"
  6. "regexp"
  7. "strings"
  8. )
  9. func main() {
  10. // (1) slurp the file
  11. data, err := ioutil.ReadFile("../tmpl/xpl.go")
  12. if err != nil {
  13. log.Fatal("ioutil.ReadFile: ", err)
  14. }
  15. s := string(data)
  16. fmt.Printf("----\n%s----\n", s)
  17. // => function that works for files of (known) other encodings that ascii or utf8
  18. // (2) create a map that maps placeholder to be replaced to the replacements
  19. x := map[string]string {
  20. "BEGIN" : "{",
  21. "END" : "}"}
  22. ks := make([]string, 0, len(x))
  23. for k := range x {
  24. ks = append(ks, k)
  25. }
  26. // => function(s) that gets the keys from maps
  27. // (3) create a regexp that finds the placeholder to be replaced
  28. p := strings.Join(ks, "|")
  29. fmt.Printf("/%s/\n", p)
  30. r := regexp.MustCompile(p)
  31. // => funny letters & order need more consideration
  32. // (4) create a callback function for ..ReplaceAllStringFunc that knows
  33. // about the map x
  34. f := func(s string) string {
  35. fmt.Printf("*** '%s'\n", s)
  36. return x
    展开收缩
  37. }
  38. // => function (?) to do Step (2) .. (4) in a reusable way
  39. // (5) do the replacing (s will be overwritten with the result)
  40. s = r.ReplaceAllStringFunc(s, f)
  41. fmt.Printf("----\n%s----\n", s)
  42. // (6) write back
  43. err = ioutil.WriteFile("result.go", []byte(s), 0644)
  44. if err != nil {
  45. log.Fatal("ioutil.WriteFile: ", err)
  46. }
  47. // => function that works for files of (known) other encodings that ascii or utf8
  48. }

output:

  1. go run 13789882.go
  2. ----
  3. func main() BEGIN
  4. END
  5. ----
  6. /BEGIN|END/
  7. *** 'BEGIN'
  8. *** 'END'
  9. ----
  10. func main() {
  11. }
  12. ----

答案3

得分: 1

如果您的文件大小很大,将所有内容读入内存可能是不可能的,也不建议这样做。可以尝试使用BytesReplacingReader,它以流式方式进行替换。而且性能还不错。如果您想替换两个字符串(例如BEGIN -> {END -> }),只需在原始读取器上包装两个BytesReplacingReader,一个用于BEGIN,一个用于END

  1. r := NewBytesReplacingReader(
  2. NewBytesReplacingReader(inputReader, []byte("BEGIN"), []byte("{"),
  3. []byte("END"), []byte("}")
  4. // 正常使用 r,所有非重叠的出现的
  5. // "BEGIN" 和 "END" 将被替换为 "{" 和 "}"
英文:

If your file size is huge, reading everything in memory might not be possible nor advised. Give BytesReplacingReader a try as it is done replacement in streaming fashion. And it's reasonably performant. If you want to replace two strings (such as BEGIN -> { and END -> }), just need to wrap two BytesReplacingReader over original reader, one for BEGIN and one for END:

  1. r := NewBytesReplacingReader(
  2. NewBytesReplacingReader(inputReader, []byte("BEGIN"), []byte("{"),
  3. []byte("END"), []byte("}")
  4. // use r normally and all non-overlapping occurrences of
  5. // "BEGIN" and "END" will be replaced with "{" and "}"

huangapple
  • 本文由 发表于 2012年12月10日 01:36:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/13789882.html
匿名

发表评论

匿名网友

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

确定