读取文本文件,替换其单词,输出到另一个文本文件

时间:2012-12-09 17:36:41

标签: string file text replace go

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

由于

这是我到目前为止的代码。

package main

import (
    "os"
    "bufio"
    "bytes"
    "io"
    "fmt"
    "strings"
)


func readLines(path string) (lines []string, errr error) {
    var (
        file *os.File
        part []byte
        prefix bool
    )
    if file, errr = os.Open(path); errr != nil {
        return
    }
    defer file.Close()

    reader := bufio.NewReader(file)
    buffer := bytes.NewBuffer(make([]byte, 0))
    for {
        if part, prefix, errr = reader.ReadLine(); errr != nil {
            break
        }
    buffer.Write(part)
        if !prefix {
            lines = append(lines, buffer.String())
            buffer.Reset()
        }
    }
    if errr == io.EOF {
        errr = nil
    }
    return
}

func writeLines(lines []string, path string) (errr error) {
    var (
        file *os.File
    )

    if file, errr = os.Create(path); errr != nil {
        return
    }
    defer file.Close()


    for _,item := range lines {

        _, errr := file.WriteString(strings.TrimSpace(item) + "\n");

        if errr != nil {

            fmt.Println(errr)
            break
        }
    }

    return
}

func FixBegin(lines []string) (errr error) {
    var(
    a string

    )
for i := 0; ; i++ {
        a = lines[i];

        fmt.Println(strings.Replace(a, "BEGIN", "{", -1))
    }

    return
}

func FixEnd(lines []string) (errr error) {
    var(
    a string

    )
for i := 0; ; i++ {
        a = lines[i];

        fmt.Println(strings.Replace(a, "END", "}", -1))
    }
    return
}

func main() {
    lines, errr := readLines("foo.txt")
    if errr != nil {
        fmt.Println("Error: %s\n", errr)
        return
    }
    for _, line := range lines {
        fmt.Println(line)
    }


    errr = FixBegin(lines)

    errr = writeLines(lines, "beer2.txt")
    fmt.Println(errr)

    errr = FixEnd(lines)
    lines, errr = readLines("beer2.txt")
    if errr != nil {
        fmt.Println("Error: %s\n", errr)
        return
    }
    errr = writeLines(lines, "beer2.txt")
    fmt.Println(errr)
}

3 个答案:

答案 0 :(得分: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)
        }

        src = bytes.Replace(src, []byte("BEGIN"), []byte("{"), -1)
        src = bytes.Replace(src, []byte("END"), []byte("}"), -1)
        if err = ioutil.WriteFile("beer2.txt", src, 0666); err != nil {
                log.Fatal(err)
        }
}
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);
}.
jnml@fsc-r630:~/src/tmp/SO/13789882$ 

答案 1 :(得分:3)

我同意@jnml wrt使用ioutil来篡改文件并将其写回来。但我认为替换不应该通过[]字节多次传递来完成。代码和数据是字符串/文本,应该这样对待(即使处理非ascii / utf8编码需要estra工作);一次性替换(所有占位符'一次')避免了替换先前更改结果的风险(即使我的正则表达式提议必须得到改进以处理非平凡的任务)。

package main

import(
    "fmt"
    "io/ioutil"
    "log"
    "regexp"
    "strings"
)

func main() {
    // (1) slurp the file
    data, err := ioutil.ReadFile("../tmpl/xpl.go")
    if err != nil {
        log.Fatal("ioutil.ReadFile: ", err)
    }
    s := string(data)
    fmt.Printf("----\n%s----\n", s)
    // => function that works for files of (known) other encodings that ascii or utf8

    // (2) create a map that maps placeholder to be replaced to the replacements
    x := map[string]string {
        "BEGIN" : "{",
        "END" : "}"}
    ks := make([]string, 0, len(x))
    for k := range x {
        ks = append(ks, k)
    }
    // => function(s) that gets the keys from maps

    // (3) create a regexp that finds the placeholder to be replaced
    p := strings.Join(ks, "|")
    fmt.Printf("/%s/\n", p)
    r := regexp.MustCompile(p)
    // => funny letters & order need more consideration

    // (4) create a callback function for ..ReplaceAllStringFunc that knows
    // about the map x
    f := func(s string) string {
        fmt.Printf("*** '%s'\n", s)
        return x[s]
    }
    // => function (?) to do Step (2) .. (4) in a reusable way

    // (5) do the replacing (s will be overwritten with the result)
    s = r.ReplaceAllStringFunc(s, f)
    fmt.Printf("----\n%s----\n", s)

    // (6) write back
    err = ioutil.WriteFile("result.go", []byte(s), 0644)
    if err != nil {
        log.Fatal("ioutil.WriteFile: ", err)
    }
    // => function that works for files of (known) other encodings that ascii or utf8
}

输出:

go run 13789882.go
----
func main() BEGIN
END
----
/BEGIN|END/
*** 'BEGIN'
*** 'END'
----
func main() {
}
----

答案 2 :(得分:0)

如果文件很大,则可能也不建议读取内存中的所有内容。尝试BytesReplacingReader,以流方式完成替换。而且性能合理。如果要替换两个字符串(例如BEGIN -> {END -> }),只需要将两个BytesReplacingReader包装在原始阅读器上,将一个包装为BEGIN,将一个包装为{{1 }}:

END