Golang 扫描文本文件中的单词

我想将一个文本文件(file.txt)拆分为两个单独的大小相等的文本文件,例如 file1.txt 和 file2.txt。我在拆分中的策略是扫描单词并计算它们,然后将前半部分的单词写入 file1.txt,其余的写入 file2.txt 这是代码:


package main

import (

    "bufio"

    "fmt"

    "log"

    "os"

)

func main() {

    WordbyWordScan()

}

func WordbyWordScan() {

    file, err := os.Open("file.txt.txt")

    if err != nil {

        log.Fatal(err)

    }

    defer file.Close()

    scanner := bufio.NewScanner(file)

    scanner.Split(bufio.ScanWords)

    count := 0

    for scanner.Scan() {

        fmt.Println(scanner.Text())

        count++

    }

    if err := scanner.Err(); err != nil {

        log.Fatal(err)

    }

    fmt.Printf("%d\n", count)

}

https://godoc.org/bufio#example-Scanner--Words


据我猜测,scanner.Scan() 返回一个布尔值。在计算了单词的数量之后,如何在 Golang 中实现这样的代码,将前半部分的单词写入 file1.txt,其余的写入 file2.txt?


ITMISS
浏览 228回答 3
3回答

慕莱坞森

解决方案:var s []string&nbsp;...for scanner.Scan() {// storing or appending file.txt string values to array s.&nbsp; &nbsp; &nbsp; &nbsp; s = append(s, scanner.Text())&nbsp; &nbsp; }&nbsp; &nbsp; if err := scanner.Err(); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; }&nbsp; &nbsp; //writing to file1 and file2&nbsp; &nbsp; if len(s)%2 == 0 { // if the occurences of words is an even number.&nbsp; &nbsp; &nbsp; &nbsp; for i := 0; i <= len(s)/2-1; i++ { // Writing first half of words to file1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Fprintln(file1, s[i])&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for j := len(s) / 2; j < len(s); j++ { // Writing second half of words to file2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Fprintln(file2, s[j])&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else { // if the occurences of words is an odd number.&nbsp; &nbsp; &nbsp; &nbsp; for i := 0; i <= len(s)/2; i++ { // Writing first part of words to file1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Fprintln(file1, s[i])&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for j := len(s)/2 + 1; j < len(s); j++ { // Writing second part of words to file2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Fprintln(file2, s[j])&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }...

大话西游666

package mainimport (&nbsp; &nbsp; "bufio"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "log"&nbsp; &nbsp; "os")func main() {&nbsp; &nbsp; WordbyWordScan()}func WordbyWordScan() {&nbsp; &nbsp; file, err := os.Open("file.txt.txt")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; }&nbsp; &nbsp; file1, err := os.Create("file1.txt.txt")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; file2, err := os.Create("file2.txt.txt")&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; panic(err)&nbsp; &nbsp; }&nbsp; &nbsp; defer file.Close()&nbsp; &nbsp; defer file1.Close()&nbsp; &nbsp; defer file2.Close()&nbsp; &nbsp; file.Seek(0, 0)&nbsp; &nbsp; scanner := bufio.NewScanner(file)&nbsp; &nbsp; scanner.Split(bufio.ScanWords)&nbsp; &nbsp; w := 0&nbsp; &nbsp; for scanner.Scan() {&nbsp; &nbsp; &nbsp; &nbsp; var outfile *os.File&nbsp; &nbsp; &nbsp; &nbsp; if w%2 == 0 {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outfile = file1&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outfile = file2&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; fmt.Fprintln(outfile, scanner.Text())&nbsp; &nbsp; &nbsp; &nbsp; w++&nbsp; &nbsp; }&nbsp; &nbsp; if err := scanner.Err(); err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; }}

慕后森

如果您想将文件切成两半,那么您已经完成了一半。数完单词后,只需返回并再次读取文件,将一半写入一个文件,一半写入另一个文件:file.Seek(0,0)scanner = bufio.NewScanner(file)scanner.Split(bufio.ScanWords)w:=0for scanner.Scan() {&nbsp; &nbsp;var outfile *os.File&nbsp; &nbsp;if w<count/2 {&nbsp; &nbsp; &nbsp;outfile=file1&nbsp; &nbsp;} else {&nbsp; &nbsp; &nbsp;outfile=file2&nbsp; &nbsp;}&nbsp; &nbsp;fmt.Fprintln(outfile,scanner.Text())&nbsp; &nbsp;w++}上面,file1和file2是两个输出文件。如果您不需要将文件切成两半而只需将一半的单词放在一个文件中,另一半放在另一个文件中,您可以一次完成,无需计数。当您从第一个读取时,只需切换要写入的文件:w:=0for scanner.Scan() {&nbsp; &nbsp;var outfile *os.File&nbsp; &nbsp;if w%2==0 {&nbsp; &nbsp; &nbsp;outfile=file1&nbsp; &nbsp;} else {&nbsp; &nbsp; &nbsp;outfile=file2&nbsp; &nbsp;}&nbsp; &nbsp;fmt.Fprintln(outfile,scanner.Text())&nbsp; &nbsp;w++}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go