Memo: [Java] Process the read csv (extract, change according to the conditions) and output

background

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.

haikei.png

** Such data **

** 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

Thing you want to do

** 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.

** Completed code **

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();
        }
    }
}

**: pencil: What you are doing **

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 ❻


**: pencil: memo **

** Pattern and Matcher used to search **

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
** Scientific calculator **

Note that I used a calculator function other than the standard Windows calculator for the first time. 電卓.png

When you want too much when you divide 101014.45 by 10000 101014.45 Mod 10000 = 1014.45 関数電卓.png

Recommended Posts

Memo: [Java] Process the read csv (extract, change according to the conditions) and output
[Java] Change the process according to the situation with the Strategy pattern
I tried to read and output CSV with Outsystems
Read the first 4 bytes of the Java class file and output CAFEBABE
[Java] How to output and write files!
[Java 7] Divide the Java list and execute the process
Output of the book "Introduction to Java"
[Java] Color the standard output to the terminal
[Java] How to get and output standard input
[Java] Memo on how to write the source
A memo about the types of Java O/R mappers and how to select them
<java> Read Zip file and convert directly to string
[No.007] Organization management screen and login process to the organization
To implement, write the test and then code the process
[Java] How to extract the file name from the path
Java memory management and how to read GC Viewer
Java classes and instances to understand in the figure
Gzip-compress byte array in Java and output to file
[Note] Java Output of the sum of odd and even elements
[Java] Change language and locale to English in JVM options
Difference between Java and JavaScript (how to find the average)
Command to check the number and status of Java threads
I want to change the log output settings of UtilLoggingJdbcLogger
Change the file name and output destination of the JavaVM error file
Correct the character code in Java and read from the URL
[Java] Use ResolverStyle.LENIENT to handle the date and time nicely
Convert Excel to Blob with java, save it, read it from DB and output it as a file!
Read the data of Shizuoka point cloud DB with Java and try to detect the tree height.