解析大型CSV文件,其中我只需要两列中的值(Java)
我有一个CSV文件的以下部分,有7列(见第一行),我想把日期(第1列)作为树状图中的键,把Adj Close值(第7列)作为树状图中的映射值: 日期、打开、高、低、关闭、音量、调整关闭 7/1/2011132.09134.1131.78133.92202370700133.92 6/30/2011,131.14,132.18,130.71,131.97,223496600,131.97 6/29/2011,130.2,130.93,129.63,130.72,244295500,130.72 2011年6月28日1128.45129.63128.27129.6116556300129.61 在作业的前一部分中,我只需将开放值(第2列)作为映射值(日期是键)放在树状图中。我为此使用了扫描仪,代码如下:解析大型CSV文件,其中我只需要两列中的值(Java),csv,Csv,我有一个CSV文件的以下部分,有7列(见第一行),我想把日期(第1列)作为树状图中的键,把Adj Close值(第7列)作为树状图中的映射值: 日期、打开、高、低、关闭、音量、调整关闭 7/1/2011132.09134.1131.78133.92202370700133.92 6/30/2011,131.14,132.18,130.71,131.97,223496600,131.97 6/29/2011,130.2,130.93,129.63,130.72,244295500,130.72 2
TreeMap<String, String> loadPriceData(String fileName) throws Exception
{
TreeMap<String, String> prices = new TreeMap<String, String>();//create prices map
Scanner fileScanner = new Scanner(new File(fileName));
fileScanner.useDelimiter("[,\n]+");// use comma as delimiter
while(fileScanner.hasNext()) //condition detects comma
{
prices.put(fileScanner.nextLine(),fileScanner.nextLine());
}
return prices;
}
TreeMap loadPriceData(字符串文件名)引发异常
{
TreeMap prices=new TreeMap();//创建价格映射
Scanner fileScanner=新扫描仪(新文件(文件名));
fileScanner.useDelimiter(“[,\n]+”);//使用逗号作为分隔符
while(fileScanner.hasNext())//条件检测到逗号
{
prices.put(fileScanner.nextLine(),fileScanner.nextLine());
}
退货价格;
}
但这似乎只适用于2列CSV数据。如果我需要第7列中的映射值,什么是有效的方法?提前感谢。您的代码不起作用。分隔符模式不正确。如果您查看地图的内容,您将看到没有日期-价格映射,您只有一个奇怪的映射 与其使用扫描仪,更简单的方法是逐行读取文件,用逗号分隔每一行,并将所需的字段放入地图中 例如:
public TreeMap<String, String> loadPriceData(String fileName) throws IOException {
TreeMap<String, String> prices = new TreeMap<String, String>();// create prices map
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(fileName));
String line;
//read each line in the csv file
while ((line = in.readLine()) != null) {
//split line on comma
String[] fields = line.split(",");
//put the first and second fields into the map
prices.put(fields[0], fields[1]);
}
return prices;
} catch (IOException e) {
throw e;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {// ignore
}
}
}
}
public树映射loadPriceData(字符串文件名)引发IOException{
TreeMap prices=new TreeMap();//创建价格映射
BufferedReader in=null;
试一试{
in=新的BufferedReader(新的文件读取器(文件名));
弦线;
//读取csv文件中的每一行
而((line=in.readLine())!=null){
//逗号分隔线
String[]fields=line.split(“,”);
//将第一个和第二个字段放入地图
价格。看跌期权(字段[0],字段[1]);
}
退货价格;
}捕获(IOE异常){
投掷e;
}最后{
if(in!=null){
试一试{
in.close();
}捕获(IOE异常){//忽略
}
}
}
}
如果您使用的是Java 7,则可以使用try with resources语句:
public TreeMap<String, String> loadPriceData(String fileName) throws IOException {
TreeMap<String, String> prices = new TreeMap<>();// create prices map
try (BufferedReader in = Files.newBufferedReader(Paths.get(fileName}),
Charset.forName("UTF-8"))) {
String line;
//read each line in the csv file
while ((line = in.readLine()) != null) {
//split line on comma
String[] fields = line.split(",");
//put the first and second fields into the map
prices.put(fields[0], fields[1]);
}
return prices;
} catch (IOException e) {
throw e;
}
}
public树映射loadPriceData(字符串文件名)引发IOException{
TreeMap prices=new TreeMap();//创建价格映射
try(BufferedReader in=Files.newBufferedReader(path.get(fileName})),
字符集forName(“UTF-8”)){
弦线;
//读取csv文件中的每一行
而((line=in.readLine())!=null){
//逗号分隔线
String[]fields=line.split(“,”);
//将第一个和第二个字段放入地图
价格。看跌期权(字段[0],字段[1]);
}
退货价格;
}捕获(IOE异常){
投掷e;
}
}
您的代码不起作用。分隔符模式不正确。如果您查看地图的内容,您将看到没有日期-价格映射,您只有一个奇怪的映射
与其使用扫描仪,更简单的方法是逐行读取文件,用逗号分隔每一行,并将所需的字段放入地图中
例如:
public TreeMap<String, String> loadPriceData(String fileName) throws IOException {
TreeMap<String, String> prices = new TreeMap<String, String>();// create prices map
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(fileName));
String line;
//read each line in the csv file
while ((line = in.readLine()) != null) {
//split line on comma
String[] fields = line.split(",");
//put the first and second fields into the map
prices.put(fields[0], fields[1]);
}
return prices;
} catch (IOException e) {
throw e;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {// ignore
}
}
}
}
public树映射loadPriceData(字符串文件名)引发IOException{
TreeMap prices=new TreeMap();//创建价格映射
BufferedReader in=null;
试一试{
in=新的BufferedReader(新的文件读取器(文件名));
弦线;
//读取csv文件中的每一行
而((line=in.readLine())!=null){
//逗号分隔线
String[]fields=line.split(“,”);
//将第一个和第二个字段放入地图
价格。看跌期权(字段[0],字段[1]);
}
退货价格;
}捕获(IOE异常){
投掷e;
}最后{
if(in!=null){
试一试{
in.close();
}捕获(IOE异常){//忽略
}
}
}
}
如果您使用的是Java 7,则可以使用try with resources语句:
public TreeMap<String, String> loadPriceData(String fileName) throws IOException {
TreeMap<String, String> prices = new TreeMap<>();// create prices map
try (BufferedReader in = Files.newBufferedReader(Paths.get(fileName}),
Charset.forName("UTF-8"))) {
String line;
//read each line in the csv file
while ((line = in.readLine()) != null) {
//split line on comma
String[] fields = line.split(",");
//put the first and second fields into the map
prices.put(fields[0], fields[1]);
}
return prices;
} catch (IOException e) {
throw e;
}
}
public树映射loadPriceData(字符串文件名)引发IOException{
TreeMap prices=new TreeMap();//创建价格映射
try(BufferedReader in=Files.newBufferedReader(path.get(fileName})),
字符集forName(“UTF-8”)){
弦线;
//读取csv文件中的每一行
而((line=in.readLine())!=null){
//逗号分隔线
String[]fields=line.split(“,”);
//将第一个和第二个字段放入地图
价格。看跌期权(字段[0],字段[1]);
}
退货价格;
}捕获(IOE异常){
投掷e;
}
}
谢谢,这似乎已将我需要的数据输入到我的树状图中。然而,在我使用您的第一个建议解析CSV文件之后,我很困惑为什么我的树映射没有按照CSV文件中显示的排序顺序。如果你看一下上面我的CSV文件的前几行,日期是递减的。但是,当我使用迭代器在树形图中迭代并打印键和值时,结果是完全不同的顺序。你知道为什么会这样吗?而且,第一行中的标题似乎也不在我的树状图中。谢谢。这张地图是按照按键的“自然顺序”排序的。您的键是字符串,因此映射将执行字符串比较以进行排序