dongyinshua9996 2018-04-21 18:51
浏览 46

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

I wrote this code to get the list of the file in directory, appending the names in a slice and one by one open them, after I open a file I search for some words in the file and if found write them in a new file. But I always get the same words in the new files and I can't figure out why

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, "
")
        }
        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条回答 默认 最新

  • duanpai1920 2018-04-22 13:51
    关注

    It's hard to be sure, since your code is difficult to read, but this looks particularly suspicious (in pseudocode),

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

    There are other problems with your code.


    Here's a more readable example (a first draft), of what you appear to be trying to do (Python modules in Python files?):

    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("
    ")
        }
        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)
            }
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图