The workplace collects data on the atmosphere and ultraviolet rays with a Raspberry Pi. If you want to borrow the data and use it as a sample to draw a graph in the project you are doing now, Since weird data was mixed in the middle, I will write down the code that I wrote that it can not be used unless it is formatted.
** Get data every second from 0:00 to 23:59:59 at yyyy / mm / dd hh: mm: ss, atmospheric pressure (hPa), temperature (℃)
**
Occasionally, the atmospheric pressure data with 10 at the beginning is mixed.
20180712.csv
2018/07/12 00:00:00,1014.44,28.59
2018/07/12 00:00:01,1014.44,28.59
2018/07/12 00:00:02,1014.46,28.62
2018/07/12 00:00:03,1014.45,28.62
2018/07/12 00:00:04,1014.44,28.59
2018/07/12 00:00:05,1014.45,28.62
2018/07/12 00:00:06,101014.45,28.59
2018/07/12 00:00:07,1014.45,28.62
~ Omitted ~
2018/07/12 23:59:58,1006.29,28.66
2018/07/12 23:59:59,1006.28,28.62
** I want to read CSV, perform necessary processing, and output to another name CSV. ** ** -I don't need every second, so I want to write out data every minute. (Extract only 00 seconds per minute) -If the atmospheric pressure data for 00 seconds contains a value such as 101014.45, I would like to change it to 1014.45 and write the data.
test.java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class test{
public static void main(String[] args) {
process("20180712.csv", ":00,","output.csv");
}
public static void process(String read_file, String searchString, String output_file){
try(FileReader fr = new FileReader(read_file);BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter(output_file);BufferedWriter bw = new BufferedWriter(fw)){
String line;
while ((line = br.readLine()) != null) {
Pattern p = Pattern.compile(searchString);
Matcher m = p.matcher(line);
if (m.find()){
String[] csvArray;
csvArray = line.split(",");
String time = csvArray[0];
float press = Float.parseFloat(csvArray[1])%10000;
String pressure = String.format("%.2f", press);
String temperature = csvArray[2];
String outputLine = String.join(",",time,pressure,temperature);
bw.write(outputLine);
bw.newLine();
}else{
}
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
We are passing three arguments: the file name to be read into the process, the search character, and the output file name. process("20180712.csv", ":00,","output.csv");
❶ Search for rows in 0 seconds (": 00,") using Pattern and Matcher ❷ When a row containing 00 seconds is found, split it into an array (csvArray) of String with, (comma). ❸ Atmospheric pressure data (csvArray [1]) is a decimal number, so convert from String type to float type.
Repeat for the number of lines of the file to read from ❶ to ❻
Matcher can be created by passing a string to the Pattern.matcher () method. The result of the match judgment is returned as true or false.
Pattern pattern = Pattern.compile("U"); //The character string you want to search"U"Create a Pattern to set
Matcher matcher = pattern.matcher("AIUEO"); //Character string to be searched"AIUEO"Create a matcher that searches for
matcher.find() //Judgment of partial match
matcher.lookingAt() //Judgment of whether to match from the beginning
matcher.matches() //Judgment of perfect match
Note that I used a calculator function other than the standard Windows calculator for the first time.
When you want too much when you divide 101014.45 by 10000 101014.45 Mod 10000 = 1014.45
Recommended Posts