使用 Go 从 csv 数据框的列创建文本文件

我正在尝试遍历csv文件并输出标题为第一列中每一行的文本文件。然后用该列的其他行中的数据填充每个文本文件。我能够将内容打印csv到文本文件中,但是我无法使用 for 循环来获取逻辑以获取第一列的索引并使用它来创建/命名一个新.txt文件。


package main


import (

    "fmt"

    "io"

    "io/ioutil"

    "log"

    "os"

)


func main() {

    fmt.Println("Enter file path to CSV: ")

    var csvFile string

    _, err := fmt.Scanln(&csvFile)

    if err != nil {

        log.Fatal("Cannot read input")

        return

    }

    //open file

    inFile, err := os.Open(csvFile)

    if err != nil {

        log.Fatal(err)

    }

    defer inFile.Close()


    readMe, _ := ioutil.ReadAll(inFile)


    blankFile, err := os.Create(`C:\temp\test.txt`)

    if err != nil {

        log.Fatal(err)

    }

    defer blankFile.Close()


    //write data to text file

    outFile, err := blankFile.Write(readMe)

    if err == io.EOF {

        log.Fatalln("Failed")

    } else if err != nil {

        log.Fatal(err)

    }

    //print bytes total

    fmt.Println(outFile, " bytes printed")


}


至尊宝的传说
浏览 151回答 1
1回答

慕斯709654

从 csv 中获取多列并将每一列打印到一个新的文本文件中。循环一个 csv 并生成一个新的文本文件,该文件将在第 1 行中的每一列之后命名。然后将使用该列的其他行中的数据填充每个文本文件。例如,package mainimport (&nbsp; &nbsp; "encoding/csv"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "io"&nbsp; &nbsp; "os"&nbsp; &nbsp; "path/filepath")func CsvFileToTxtFiles(inFile string) error {&nbsp; &nbsp; in, err := os.Open(inFile)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return err&nbsp; &nbsp; }&nbsp; &nbsp; defer in.Close()&nbsp; &nbsp; r := csv.NewReader(in)&nbsp; &nbsp; hdr, err := r.Read()&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; return err&nbsp; &nbsp; }&nbsp; &nbsp; f := make([]*os.File, len(hdr))&nbsp; &nbsp; w := make([]*csv.Writer, len(hdr))&nbsp; &nbsp; pfx := filepath.Clean(inFile)&nbsp; &nbsp; pfx = pfx[:len(pfx)-len(filepath.Ext(pfx))]&nbsp; &nbsp; for i, col := range hdr {&nbsp; &nbsp; &nbsp; &nbsp; var err error&nbsp; &nbsp; &nbsp; &nbsp; f[i], err = os.Create(pfx + "." + col + ".txt")&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return err&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; defer f[i].Close()&nbsp; &nbsp; &nbsp; &nbsp; w[i] = csv.NewWriter(f[i])&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return err&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; defer w[i].Flush()&nbsp; &nbsp; }&nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; row, err := r.Read()&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if row == nil && err == io.EOF {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return err&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for i, col := range row {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; err := w[i].Write([]string{col})&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return err&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; for i := range hdr {&nbsp; &nbsp; &nbsp; &nbsp; var err error&nbsp; &nbsp; &nbsp; &nbsp; w[i].Flush()&nbsp; &nbsp; &nbsp; &nbsp; err = w[i].Error()&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return err&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; err = f[i].Close()&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return err&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return nil}func main() {&nbsp; &nbsp; if len(os.Args) <= 1 {&nbsp; &nbsp; &nbsp; &nbsp; usage := "usage: " + filepath.Base(os.Args[0]) + " FILE"&nbsp; &nbsp; &nbsp; &nbsp; fmt.Fprintln(os.Stderr, usage)&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }&nbsp; &nbsp; inFile := os.Args[1]&nbsp; &nbsp; err := CsvFileToTxtFiles(inFile)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Fprintln(os.Stderr, err)&nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; }}输出:$ cat ioj.test.csvone,two,three1,2,311,22,33$ go run ioj.go ioj.test.csv$ cat ioj.test.one.txt111$ cat ioj.test.two.txt222$ cat ioj.test.three.txt333$&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go