在 Java 中存储每第 n 行
Storing every nth row in Java
您好,我不确定如何将 csv 文件中的每一行作为一个元素存储在 Java 中,以便我可以检查同一列中的前一个元素。有人知道怎么做吗?
这是我正在尝试做的事情:
我正在处理看起来像 this
的数据框
这是它的样子:
shape id day hour week id footfall category area name
22496 22/3/14 3 12 634 Work cluster CBD area 1
22670 22/3/14 3 12 220 Shopping cluster Orchard Road 1
23287 22/3/14 3 12 723 Airport Changi Airport 2
16430 22/3/14 4 12 947 Work cluster CBD area 2
4697 22/3/14 3 12 220 Residential area Ang Mo Kio 2
4911 22/3/14 3 12 1001 Shopping cluster Orchard Rd 3
11126 22/3/14 3 12 220 Residential area Ang Mo Kio 2
等等...直到 635 行 return。
我想将每个 hour
存储为元素,以便我可以将它与我的其他数据集进行比较。
这是我目前所做的:
package network;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class footfall {
public static void main(String[] args) throws IOException {
ArrayList<String> list = new ArrayList<>();
// New BufferedReader.
BufferedReader reader = new BufferedReader(new FileReader(
"C:\only18date.csv"));
// Add all lines from file to ArrayList.
while (true) {
String line = reader.readLine();
if (line == null) {
break;
}
list.add(line);
}
// Close it.
reader.close();
// Print size of ArrayList.
System.out.println("Lines: " + list.size());
// Print each line.
int size = list.size();
for(int i=0; i < size; i++) {
String[] data=(list.get(i)).split(",");
String n= data[3];
System.out.println(n);
} // so i can check every row with the previous value from the column. // & in order for me to check the previous value i can simply write n-1 or //something like that but im not sure how to do it :/
// i want to store every nth row from column 3 as an element
}
}
}
您可以使用正则表达式以快速而肮脏的方式做到这一点。
示例(注意:为了独立起见,它与您的代码略有不同)
// emulating csv file
String[] lines = {
"shape id day hour week id footfall category area name",
"22496 22/3/14 3 12 634 Work cluster CBD area 1",
"22670 22/3/14 3 12 220 Shopping cluster Orchard Road 1",
"23287 22/3/14 3 12 723 Airport Changi Airport 2",
"16430 22/3/14 4 12 947 Work cluster CBD area 2",
"4697 22/3/14 3 12 220 Residential area Ang Mo Kio 2",
"4911 22/3/14 3 12 1001 Shopping cluster Orchard Rd 3",
"11126 22/3/14 3 12 220 Residential area Ang Mo Kio 2"
};
List<Integer> hours = new ArrayList<Integer>();
// | begin of input
// || any number of characters, reluctantly quantified
// || | 1+ whitespace
// || | | single digit in group 1 (the hours)
// || | | | 1+ whitespace
Pattern p = Pattern.compile("^.+?\s+(\d)\s+");
// here you probably want to only add the m.group(1) to your list, possibly as integer as it's done here
for (String s: lines) {
Matcher m = p.matcher(s);
if (m.find()) {
hours.add(Integer.parseInt(m.group(1)));
}
}
System.out.println(hours);
输出
[3, 3, 3, 4, 3, 3, 3]
备注
上面的示例要求您的数据保持一致。
如果您的工作时间可以 > 9
,您可能希望将模式的 (\d)
部分更改为 (\d{1,2})
。
你可以这样解决:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class footfall {
public static void main(String[] args) throws IOException {
ArrayList<String> list = new ArrayList<>();
// New BufferedReader.
BufferedReader reader = new BufferedReader(new FileReader(
"C:\idafootfall.csv"));
// Add all lines from file to ArrayList.
String line;
while ((line=reader.readLine())!=null)
list.add(line);
// Close it.
reader.close();
BufferedReader reader2 = new BufferedReader(new FileReader(
"C:\category.csv"));
// Add all lines from file to ArrayList.
Map<String,Category> map=new HashMap<String, Category>();
String line2;
while ((line2=reader2.readLine())!=null){
String[] split=line2.split(",");
try
{
map.put(split[0], new Category(Double.valueOf(split[1]),Double.valueOf(split[2])));
}
catch(NumberFormatException e)
{
}
}
// Close it.
reader2.close();
// Print size of ArrayList.
System.out.println("Lines: " + list.size());
// Print each line.
int size = list.size();
int previousHour=0;
List<String> elements = new ArrayList<String>();
for (int i = 0; i < size; i++) {
try
{
String[] data = (list.get(i)).split(",");
System.out.println(data[2]);
double newHourSum = Integer.parseInt(data[2]) - previousHour;
double footfallHour = ( Integer.parseInt(data[2])/(newHourSum-previousHour)) * map.get(data[0]).foreigners;
previousHour=Integer.parseInt(data[2]);
// i want to store every nth row from column 3 as an element
} catch(NumberFormatException e){}
}
}
}
class Category{
Double foreigners;
Double locals;
public Category(Double foreigners,Double locals){
this.foreigners=foreigners;
this.locals=locals;
}
}
现在我已经实现了类似于 link 版本的代码。
希望对你有帮助。
不太确定你想要什么,但看看这是否有帮助。
for (int i = 0; i < size; i+=n) { // Notice the i+=n to get every nth row.
String[] data = (list.get(i)).split(",");
System.out.println(data[3]);
// i want to store every nth row from column 3 as an element
}
注意:您可以更改 i
的初始值以调整您需要的确切行。
您好,我不确定如何将 csv 文件中的每一行作为一个元素存储在 Java 中,以便我可以检查同一列中的前一个元素。有人知道怎么做吗?
这是我正在尝试做的事情:
我正在处理看起来像 this
的数据框这是它的样子:
shape id day hour week id footfall category area name
22496 22/3/14 3 12 634 Work cluster CBD area 1
22670 22/3/14 3 12 220 Shopping cluster Orchard Road 1
23287 22/3/14 3 12 723 Airport Changi Airport 2
16430 22/3/14 4 12 947 Work cluster CBD area 2
4697 22/3/14 3 12 220 Residential area Ang Mo Kio 2
4911 22/3/14 3 12 1001 Shopping cluster Orchard Rd 3
11126 22/3/14 3 12 220 Residential area Ang Mo Kio 2
等等...直到 635 行 return。
我想将每个 hour
存储为元素,以便我可以将它与我的其他数据集进行比较。
这是我目前所做的:
package network;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class footfall {
public static void main(String[] args) throws IOException {
ArrayList<String> list = new ArrayList<>();
// New BufferedReader.
BufferedReader reader = new BufferedReader(new FileReader(
"C:\only18date.csv"));
// Add all lines from file to ArrayList.
while (true) {
String line = reader.readLine();
if (line == null) {
break;
}
list.add(line);
}
// Close it.
reader.close();
// Print size of ArrayList.
System.out.println("Lines: " + list.size());
// Print each line.
int size = list.size();
for(int i=0; i < size; i++) {
String[] data=(list.get(i)).split(",");
String n= data[3];
System.out.println(n);
} // so i can check every row with the previous value from the column. // & in order for me to check the previous value i can simply write n-1 or //something like that but im not sure how to do it :/
// i want to store every nth row from column 3 as an element
}
}
}
您可以使用正则表达式以快速而肮脏的方式做到这一点。
示例(注意:为了独立起见,它与您的代码略有不同)
// emulating csv file
String[] lines = {
"shape id day hour week id footfall category area name",
"22496 22/3/14 3 12 634 Work cluster CBD area 1",
"22670 22/3/14 3 12 220 Shopping cluster Orchard Road 1",
"23287 22/3/14 3 12 723 Airport Changi Airport 2",
"16430 22/3/14 4 12 947 Work cluster CBD area 2",
"4697 22/3/14 3 12 220 Residential area Ang Mo Kio 2",
"4911 22/3/14 3 12 1001 Shopping cluster Orchard Rd 3",
"11126 22/3/14 3 12 220 Residential area Ang Mo Kio 2"
};
List<Integer> hours = new ArrayList<Integer>();
// | begin of input
// || any number of characters, reluctantly quantified
// || | 1+ whitespace
// || | | single digit in group 1 (the hours)
// || | | | 1+ whitespace
Pattern p = Pattern.compile("^.+?\s+(\d)\s+");
// here you probably want to only add the m.group(1) to your list, possibly as integer as it's done here
for (String s: lines) {
Matcher m = p.matcher(s);
if (m.find()) {
hours.add(Integer.parseInt(m.group(1)));
}
}
System.out.println(hours);
输出
[3, 3, 3, 4, 3, 3, 3]
备注
上面的示例要求您的数据保持一致。
如果您的工作时间可以 > 9
,您可能希望将模式的 (\d)
部分更改为 (\d{1,2})
。
你可以这样解决:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class footfall {
public static void main(String[] args) throws IOException {
ArrayList<String> list = new ArrayList<>();
// New BufferedReader.
BufferedReader reader = new BufferedReader(new FileReader(
"C:\idafootfall.csv"));
// Add all lines from file to ArrayList.
String line;
while ((line=reader.readLine())!=null)
list.add(line);
// Close it.
reader.close();
BufferedReader reader2 = new BufferedReader(new FileReader(
"C:\category.csv"));
// Add all lines from file to ArrayList.
Map<String,Category> map=new HashMap<String, Category>();
String line2;
while ((line2=reader2.readLine())!=null){
String[] split=line2.split(",");
try
{
map.put(split[0], new Category(Double.valueOf(split[1]),Double.valueOf(split[2])));
}
catch(NumberFormatException e)
{
}
}
// Close it.
reader2.close();
// Print size of ArrayList.
System.out.println("Lines: " + list.size());
// Print each line.
int size = list.size();
int previousHour=0;
List<String> elements = new ArrayList<String>();
for (int i = 0; i < size; i++) {
try
{
String[] data = (list.get(i)).split(",");
System.out.println(data[2]);
double newHourSum = Integer.parseInt(data[2]) - previousHour;
double footfallHour = ( Integer.parseInt(data[2])/(newHourSum-previousHour)) * map.get(data[0]).foreigners;
previousHour=Integer.parseInt(data[2]);
// i want to store every nth row from column 3 as an element
} catch(NumberFormatException e){}
}
}
}
class Category{
Double foreigners;
Double locals;
public Category(Double foreigners,Double locals){
this.foreigners=foreigners;
this.locals=locals;
}
}
现在我已经实现了类似于 link 版本的代码。 希望对你有帮助。
不太确定你想要什么,但看看这是否有帮助。
for (int i = 0; i < size; i+=n) { // Notice the i+=n to get every nth row.
String[] data = (list.get(i)).split(",");
System.out.println(data[3]);
// i want to store every nth row from column 3 as an element
}
注意:您可以更改 i
的初始值以调整您需要的确切行。