读取一个文本文件,替换其单词,输出到另一个文本文件 [英] Read a text file, replace its words, output to another text file

查看:64
本文介绍了读取一个文本文件,替换其单词,输出到另一个文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

谢谢

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

Here is the code I have so far.

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)
}

推荐答案

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$ 

这篇关于读取一个文本文件,替换其单词,输出到另一个文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆