如何从CSV文件读取到二维数组 [英] How to read from CSV file to two dimensional array

查看:910
本文介绍了如何从CSV文件读取到二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在csv文件中有这些值:



4.7,3.6,3.7,1.5

6.3,7.3,2.6,9.4 < br $> b $ b 3.6,1.5,3.5,1.5

7.2,5.3,2.5,5.3



我怎样才能存储这些二维数组中的值

解决方案

我不确定你的问题是什么...



你声明并实例化一个适当数据类型的4 x 4数组 - double?

你声明并初始化一个整数变量,对于行

你打开,然后阅读包含值的文件,逐行,也许是一个字符串

对于每个包含一行csv值的字符串: -

a)你'拆分'该行(提示)基于','

b)你循环分割值并将它们分配给当前行的列,即将列数从0增加到3

c)你增加行数

结束 -



上面没有错误处理 - 但它是你需要做的事情的要点 - 你为什么不写一些代码并试一试 - 这并不难 - 用你的代码和任何具体问题更新你的问题,你可能会得到更多的帮助

i have these values in csv file:

4.7,3.6,3.7,1.5
6.3,7.3,2.6,9.4
3.6,1.5,3.5,1.5
7.2,5.3,2.5,5.3

how can i store these values in 2 dimensional array

解决方案

I'm not sure what your issue is ...

You declare and instantiate a 4 x 4 array of the appropriate data type - double ?
You declare and initialise an integer variable, for the row
You open, then read the file containing the values, line by line, maybe into a string
For each string holding a line of 'csv' values :-
a) you 'split' the line (hint) based on the ','
b) you loop through the split values and assign them to the columns for the current row, ie incrementing the column count from 0 to 3
c) you increment the row count
End-For

There's no error handling in the above - but its the gist of what you need to do - why don't you write some code and give it a go - its not hard - update your question with the code and any specific issues you have and you'll likely get more help


这篇关于如何从CSV文件读取到二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆