Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 我不能像我想的那样返回字符串片段。刚刚通过最后一个_String_Go_Append_Slice - Fatal编程技术网

String 我不能像我想的那样返回字符串片段。刚刚通过最后一个

String 我不能像我想的那样返回字符串片段。刚刚通过最后一个,string,go,append,slice,String,Go,Append,Slice,我编写这段代码是为了获取目录中的文件列表,将名称添加到一个片段中,然后逐个打开它们。打开文件后,我在文件中搜索一些单词,如果找到,则将它们写入一个新文件。 但我总是在新文件中找到相同的单词,我不知道为什么 package main import ( "bufio" "fmt" "io/ioutil" "log" "os" "strings" "time" ) const dir_to_read_path string = "path"

我编写这段代码是为了获取目录中的文件列表,将名称添加到一个片段中,然后逐个打开它们。打开文件后,我在文件中搜索一些单词,如果找到,则将它们写入一个新文件。 但我总是在新文件中找到相同的单词,我不知道为什么

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

}

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

// 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打开一个文件 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
}

为什么不遵循围棋的标准语法呢。只是好奇。如果所有人都遵循相似的规则,世界就会变得更容易。我是新手,我需要一些解决上述问题的建议