我不能像我想的那样返回字符串切片。只传递最后一个

时间:2018-04-21 18:51:06

标签: string go append slice

我编写了这段代码来获取目录中的文件列表,将名称附加到切片中并逐个打开它们,打开文件后我搜索文件中的某些单词,如果找到则将它们写入新文件。 但我总是在新文件中得到相同的单词,我无法弄清楚为什么

package main

import (
    "bufio"
    "fmt"
    "io/ioutil"
    "log"
    "os"
    "strings"
    "time"
)

const dir_to_read_path string = "path"

func main() {

    start := time.Now()
    temp_string_filename := ""
    temp_string_filename_counter := 0

    //defer list_file()

    // just pass the file name
    for k := range list_file() {

        temp_string_filename = list_file()[temp_string_filename_counter]
        if true {
            k = k
        }
        temp_string_filename_counter++

        b, err := ioutil.ReadFile(temp_string_filename)
        if err != nil {
            fmt.Print(err)
        }

        // convert content to a 'string'
        str := string(b)
        control_params := []string{"numpy", "grabscreen", "cv2", "time", "os", "pandas", "tqdm", "collections", "models", "random", "inception_v3", "googlenet", "shuffle", "getkeys", "tflearn", "directkeys", "statistics", "motion", "tflearn.layers.conv", "conv_2d", "max_pool_2d", "avg_pool_2d", "conv_3d", "max_pool_3d", "avg_pool_3d"}

        temp_string_filename = dir_to_read_path + "output_" + temp_string_filename

        fmt.Println("Writing file n. ", k)

        file, err := os.Create(temp_string_filename)
        if err != nil {
            log.Fatal("Cannot create file", err)
        }

        for _, z := range isValueInList(control_params, str, list_file()) {

            fmt.Fprintf(file, z)
            fmt.Fprintf(file, "\n")
        }
        defer file.Close()

        elapsed := time.Since(start)
        log.Printf("Execution took %s", elapsed)
    }

}

func isValueInList(list []string, file_string string, read_file []string) []string {

    encountered_modules := make([]string, 0, 10)
    temp_string_filename := ""
    temp_string_filename_counter := 0

    encountered := map[string]bool{}
    result := make([]string, 0, 10)
    final_result := [][]string{}

    for z := range read_file {

        fmt.Println("Reading file n. ", z)

        temp_string_filename = read_file[temp_string_filename_counter]
        f, _ := os.Open(temp_string_filename)
        defer f.Close()

        scanner := bufio.NewScanner(f)
        scanner.Split(bufio.ScanWords)

        for scanner.Scan() {
            line := scanner.Text()
            for _, v := range list {
                if v == line {
                    encountered_modules = append(encountered_modules, line)
                }
            }
        }

        for v := range encountered_modules {
            if encountered[encountered_modules[v]] == true {
                // Do not add duplicate.
            } else {
                // Record this element as an encountered element.
                encountered[encountered_modules[v]] = true
                result = append(result, encountered_modules[v])
            }
        }
        temp_string_filename_counter++
        final_result = append(final_result, result)
    }
    return result

}

func list_file() []string {

    files_names := make([]string, 0, 10)

    files, err := ioutil.ReadDir("./")
    if err != nil {
        log.Fatal(err)
    }

    for _, f := range files {
        if strings.HasSuffix(f.Name(), ".txt") {
            files_names = append(files_names, string(f.Name()))

        }
    }

    return files_names

}

2 个答案:

答案 0 :(得分:0)

很难确定,因为您的代码难以阅读,但这看起来特别可疑(在伪代码中),

// main
for each file in list_file() {
    result = {
        // isValueInList
        var result
        for each file in list_file() {
            for each word in file {
                if word in wordlist and not in result {
                    result = append(result, word)
                }
            }
        }
        // all the words in wordlist in any of the files
        return result
    }
    // main
    write result
}

您的代码还有其他问题。

这是一个更易读的例子(初稿),你似乎想要做什么(Python文件中的Python模块?):

package main

import (
    "bufio"
    "bytes"
    "fmt"
    "io/ioutil"
    "os"
    "path/filepath"
)

var modules = map[string]bool{
    "numpy": true, "grabscreen": true, "cv2": true, "time": true, "os": true, "pandas": true, "tqdm": true, "collections": true,
    "models": true, "random": true, "inception_v3": true, "googlenet": true, "shuffle": true, "getkeys": true, "tflearn": true,
    "directkeys": true, "statistics": true, "motion": true, "tflearn.layers.conv": true, "conv_2d": true,
    "max_pool_2d": true, "avg_pool_2d": true, "conv_3d": true, "max_pool_3d": true, "avg_pool_3d": true,
}

func findWords(filename string, lexicon map[string]bool) error {
    f, err := os.Open(filename)
    if err != nil {
        return err
    }
    defer f.Close()

    words := make(map[string]bool)

    s := bufio.NewScanner(f)
    s.Split(bufio.ScanWords)
    for s.Scan() {
        word := s.Text()
        if _, exists := lexicon[word]; exists {
            words[word] = true
        }
    }
    if s.Err(); err != nil {
        return err
    }

    var buf bytes.Buffer
    for word := range words {
        buf.WriteString(word)
        buf.WriteString("\n")
    }
    if buf.Len() > 0 {
        err := ioutil.WriteFile(filename+`.words`, buf.Bytes(), 0666)
        if err != nil {
            return err
        }
    }
    return nil
}

func main() {
    dir := `./`
    files, err := ioutil.ReadDir(dir)
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
        os.Exit(1)
    }
    for _, file := range files {
        filename := file.Name()
        if filepath.Ext(filename) != ".py" {
            continue
        }
        findWords(filename, modules)
        if err != nil {
            fmt.Fprintln(os.Stderr, err)
        }
    }
}

答案 1 :(得分:-1)

您的代码中存在一些错误,因此我已经重写了大部分代码。 我做了什么 : 1)打开一个文件 2)读一行 3)比较它 4)检查目标文件是否存在 5)如果没有,创建它 6)如果确实如此,追加它 7)写信给它 8)关闭目标文件 9)如果有更多行,转到2 10)如果有更多文件,转到1

我试图为每个人提供可读性,以便每个人都能理解它。

package main

import (
    "bufio"
    "fmt"
    "io/ioutil"
    "log"
    "os"
    "path/filepath"
    "strconv"
    "strings"
    "time"
)

const readDir string = "./"

var startTime time.Time

func main() {
    for noFile, fileName := range listFile() {
        startTime = time.Now()
        fileInput, err := os.Open(fileName)
        if err != nil {
            log.Fatal(err)
        }
        defer fileInput.Close()

        scanner := bufio.NewScanner(fileInput)
        for scanner.Scan() {
            for _, targetContent := range []string{"numpy", "grabscreen", "cv2", "time", "os", "pandas", "tqdm", "collections", "models", "random", "inception_v3", "googlenet", "shuffle", "getkeys", "tflearn", "directkeys", "statistics", "motion", "tflearn.layers.conv", "conv_2d", "max_pool_2d", "avg_pool_2d", "conv_3d", "max_pool_3d", "avg_pool_3d"} {
                if strings.Contains(scanner.Text(), targetContent) {

                    if _, err := os.Stat(readDir + "output_" + strconv.Itoa(noFile)); os.IsNotExist(err) {
                        fmt.Println("File : " + readDir + "output_" + strconv.Itoa(noFile) + " does not exists, creating it now!")
                        createFile, err := os.Create(readDir + "output_" + strconv.Itoa(noFile))
                        if err != nil {
                            panic(err)
                        }
                        createFile.Close()
                    }

                    fileOutput, err := os.OpenFile(readDir+"output_"+strconv.Itoa(noFile), os.O_APPEND|os.O_WRONLY, 0600)
                    if err != nil {
                        panic(err)
                    }

                    if _, err = fileOutput.WriteString("contains : " + targetContent + " in : " + scanner.Text() + "\n"); err != nil {
                        panic(err)
                    }

                    fileOutput.Close()

                    fmt.Println("Writing file : ", readDir+"output_"+strconv.Itoa(noFile))
                    fmt.Println("contains : " + targetContent + " in : " + scanner.Text())
                }
            }
        }

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

        log.Printf("Execution took %s", time.Since(startTime))
    }

}

func listFile() []string {

    filesNames := make([]string, 0, 100)

    files, err := ioutil.ReadDir(readDir)
    if err != nil {
        log.Fatal(err)
    }

    for _, f := range files {
        if strings.HasSuffix(f.Name(), ".txt") {
            fileName, err := filepath.Abs(string(f.Name()))
            if err != nil {
                log.Fatal(err)
            }
            filesNames = append(filesNames, fileName)
        }
    }
    return filesNames
}