无法遍历 CSV 列
Cannot iterate through CSV columns
我正在构建一个股票筛选器,它通过 csv 文件的每一列应用计算。但是,当我 运行 for 循环时,我只得到一个结果。
String path = "C:/Users/0/Desktop/Git/Finance/Data/NQ100.csv";
Reader buf = Files.newBufferedReader(Paths.get(path));
CSVParser parsed = new CSVParser(buf, CSVFormat.DEFAULT.withFirstRecordAsHeader()
.withIgnoreHeaderCase().withTrim());
// Parse tickers
Map<String, Integer> header = parsed.getHeaderMap();
List<String> tickerList = new ArrayList<>(header.keySet());
for (int x=1; x < tickerList.size(); x++) { <----------------------- PROBLEM
// Accessing closing price by Header names
List<Double> closeList = new ArrayList<>();
for (CSVRecord record : parsed) {
String stringClose = record.get(x);
Double close = Double.valueOf(stringClose);
closeList.add(close);
}
// Percentage Change
List<Double> pctList = new ArrayList<>();
for (int i=1; i < closeList.size(); i++) {
Double pct = closeList.get(i) / closeList.get(i-1) - 1;
pctList.add(pct);
}
// Statistics
Double sum = 0.0, var = 0.0, mean, sd, rfr, sr;
// Mean
for (Double num : pctList) sum += num;
mean = sum/pctList.size();
// Standard Deviation
for (Double num: pctList) var += Math.pow(num - mean, 2);
sd = Math.sqrt(var/pctList.size());
// Risk Free Rate
rfr = Math.pow((1+0.03),(1/252.0))-1;
// Sharpe Ratio
sr = Math.sqrt(252) * ((mean-rfr)/sd);
System.out.println(tickerList.get(x) + " " + sr);
}
我的数据是这样的:
,AAL,AAPL,ADBE
2007-10-25,26.311651,23.141403,47.200001
2007-10-26,26.273216,23.384495,47.0
2007-10-29,26.004248,23.43387,47.0
所以我期待:
AAL XXX
AAPL XXX
ADBE XXX
但我得到了:
AAL 0.3604941921663456
请各位帮我找出问题所在,不胜感激!
所以这是对我有用的代码块,如果我理解你的问题,你只想 "read" 来自 csv 文件的每一列和每一行,希望能有所帮助。
br = new BufferedReader(new InputStreamReader(new FileInputStream(archivo), "UTF8"));
while ((line = br.readLine()) != null) {
if(a!=0){
String[] datos = line.split(cvsSplitBy);
System.out.println(datos[0] + " - " + datos[1] + " - " + datos[2]);
}
a++;
}
您只能在 Java 中遍历 Iterable
一次,在您的情况下 CSVParser parsed
实现 Iterable<CSVRecord>
.
因此,在分析 AAPL 和 ADBE 将作为空的处理。
您可以通过 parsed
引入 helper list init 来处理这个问题,添加下一个代码(这当然是一个单行解决方案,例如 Java 8,但此选项适用于更早的版本版本)在 for
循环之前:
List<CSVRecord> records = new ArrayList<>();
for (CSVRecord record : parsed) {
records.add(record);
}
并更改下一行:
for (CSVRecord record : records) {
与:
for (CSVRecord record : parsed) {
对于您提供的 CSV,您将获得下一个输出:
AAL -21.583101145880306
AAPL 23.417753561072438
ADBE -16.75343297000953
我正在构建一个股票筛选器,它通过 csv 文件的每一列应用计算。但是,当我 运行 for 循环时,我只得到一个结果。
String path = "C:/Users/0/Desktop/Git/Finance/Data/NQ100.csv";
Reader buf = Files.newBufferedReader(Paths.get(path));
CSVParser parsed = new CSVParser(buf, CSVFormat.DEFAULT.withFirstRecordAsHeader()
.withIgnoreHeaderCase().withTrim());
// Parse tickers
Map<String, Integer> header = parsed.getHeaderMap();
List<String> tickerList = new ArrayList<>(header.keySet());
for (int x=1; x < tickerList.size(); x++) { <----------------------- PROBLEM
// Accessing closing price by Header names
List<Double> closeList = new ArrayList<>();
for (CSVRecord record : parsed) {
String stringClose = record.get(x);
Double close = Double.valueOf(stringClose);
closeList.add(close);
}
// Percentage Change
List<Double> pctList = new ArrayList<>();
for (int i=1; i < closeList.size(); i++) {
Double pct = closeList.get(i) / closeList.get(i-1) - 1;
pctList.add(pct);
}
// Statistics
Double sum = 0.0, var = 0.0, mean, sd, rfr, sr;
// Mean
for (Double num : pctList) sum += num;
mean = sum/pctList.size();
// Standard Deviation
for (Double num: pctList) var += Math.pow(num - mean, 2);
sd = Math.sqrt(var/pctList.size());
// Risk Free Rate
rfr = Math.pow((1+0.03),(1/252.0))-1;
// Sharpe Ratio
sr = Math.sqrt(252) * ((mean-rfr)/sd);
System.out.println(tickerList.get(x) + " " + sr);
}
我的数据是这样的:
,AAL,AAPL,ADBE
2007-10-25,26.311651,23.141403,47.200001
2007-10-26,26.273216,23.384495,47.0
2007-10-29,26.004248,23.43387,47.0
所以我期待:
AAL XXX
AAPL XXX
ADBE XXX
但我得到了:
AAL 0.3604941921663456
请各位帮我找出问题所在,不胜感激!
所以这是对我有用的代码块,如果我理解你的问题,你只想 "read" 来自 csv 文件的每一列和每一行,希望能有所帮助。
br = new BufferedReader(new InputStreamReader(new FileInputStream(archivo), "UTF8"));
while ((line = br.readLine()) != null) {
if(a!=0){
String[] datos = line.split(cvsSplitBy);
System.out.println(datos[0] + " - " + datos[1] + " - " + datos[2]);
}
a++;
}
您只能在 Java 中遍历 Iterable
一次,在您的情况下 CSVParser parsed
实现 Iterable<CSVRecord>
.
因此,在分析 AAPL 和 ADBE 将作为空的处理。
您可以通过 parsed
引入 helper list init 来处理这个问题,添加下一个代码(这当然是一个单行解决方案,例如 Java 8,但此选项适用于更早的版本版本)在 for
循环之前:
List<CSVRecord> records = new ArrayList<>();
for (CSVRecord record : parsed) {
records.add(record);
}
并更改下一行:
for (CSVRecord record : records) {
与:
for (CSVRecord record : parsed) {
对于您提供的 CSV,您将获得下一个输出:
AAL -21.583101145880306
AAPL 23.417753561072438
ADBE -16.75343297000953