如何从读取文件中返回一个数组?

huangapple go评论78阅读模式
英文:

How to return an array from reading a file?

问题

我有一个CSV文件中的两列。我正在使用SearchData()函数只访问第一列。

问题是,我想将数据作为数组访问,但是当我在AccessData()函数中返回一个数组字符串,并在SearchData()中写入products[0]时,它会删除方括号[]并给我返回所有的数据;当我写入products[1]时,它会给我返回runtime error: index out of range [1] with length 1

所需结果

products[0] = 第一项
products[1] = 第二项
...
以此类推

代码

func AccessData(number int) string {
	content, err := ioutil.ReadFile("products/data1.csv")
	if err != nil {
		log.Fatal(err)
	}
	Data := string(content)
	sliceData := strings.Split(Data, ",")
	return sliceData[number]
}

func SearchData(){
    for i := 0; i <= 34; i = i + 2 {
		products := AccessData(i)
		fmt.Println(products)
	}
}
英文:

I have two columns in a CSV file. I am accessing only the first column using the SearchData() function.

The problem is that I want to access the data as an array but when I return an array string in the AccessData() function and write the products[0] in the SearchData(), it gives me all the data by removing the bracket sign [] only and when I write products[1], it gives me runtime error: index out of range [1] with length 1.

Required result

products[0] = First Item
products[1] = Second Item
...
so on

Code

func AccessData(number int) string {
	content, err := ioutil.ReadFile(&quot;products/data1.csv&quot;)
	if err != nil {
		log.Fatal(err)
	}
	Data := string(content)
	sliceData := strings.Split(Data, &quot;,&quot;)
	return sliceData[number]
}

func SearchData(){
    for i := 0; i &lt;= 34; i = i + 2 {
		products := AccessData(i)
		fmt.Println(products)
	}
}

答案1

得分: 1

这应该可以解决问题:

func firstColumns(filename string) []string {
	f, err := os.Open(filename)
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	r := csv.NewReader(f)

	var result []string
	for {
		row, err := r.Read()
		if err != nil {
			if err == io.EOF {
				break
			}

            log.Fatal(err)
		}

		if len(row) > 0 {
			result = append(result, row[0])
		}
	}

	return result
}

func main() {
    data := firstColumns("products/data1.csv")
    fmt.Println(data)
	fmt.Println(data[1])
}

这将把每一行的第一列转换为一个[]string,可以通过索引访问。

输出结果为:

[第一项 第二项]
第二项
英文:

This should do the trick:

func firstColumns(filename string) []string {
	f, err := os.Open(filename)
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	r := csv.NewReader(f)

	var result []string
	for {
		row, err := r.Read()
		if err != nil {
			if err == io.EOF {
				break
			}

            log.Fatal(err)
		}

		if len(row) &gt; 0 {
			result = append(result, row[0])
		}
	}

	return result
}

func main() {
    data := firstColumns(&quot;products/data1.csv&quot;)
    fmt.Println(data)
	fmt.Println(data[1])
}

This turns the the first column of every row into a []string which can be access index.

The output is:

[First item Second item]
Second item

huangapple
  • 本文由 发表于 2022年1月16日 00:16:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/70723193.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定