Read a csv file and process the data

In summary, the code reads data from a file into an array, extracts the headers, creates a map for future use, and then reads the data from each line to create a Region object with Municipality and Mountain Hut objects.
  • #1
DottZakapa
239
17
TL;DR Summary
Hi i need some help in this portion of code, i have no clue what it does... in particular the highlighted portion
Java:
public static Region fromFile(String name, String file) {
        Region r = new Region(name);
        List<String> lines = readData(file);
        
        String[] headers = lines.remove(0).split(";");
        Map<String, Integer> h2i = new HashMap<>();
        for (int i=0; i<headers.length; i++) {
            h2i.put(headers[i], i);
        }
        
        lines.forEach(l -> {
            String[] rows = l.split(";");
            
            String provinceName = rows[h2i.get("Province")];
            String municipalityName = rows[h2i.get("Municipality")];
            Integer municipalityAltitude = Integer.parseInt(rows[h2i.get("MunicipalityAltitude")]);
            
            Municipality municipality = r.createOrGetMunicipality(municipalityName,
                                                    provinceName, municipalityAltitude);
            
            String mh_name = rows[h2i.get("Name")];
            String altitude = rows[h2i.get("Altitude")];
            String category = rows[h2i.get("Category")];
            Integer bedsNumber = Integer.parseInt(rows[h2i.get("BedsNumber")]);
            
            
            if (altitude.equals("")) {
                r.createOrGetMountainHut(mh_name, category, bedsNumber, municipality);
            } else {
                r.createOrGetMountainHut(mh_name, Integer.parseInt(altitude), category, bedsNumber, municipality);
            }       
        });
        
        return r;
    }
 
Technology news on Phys.org
  • #2
My Java is a bit rusty. But the code looks very readable to me. This is what it does:
  1. Read the data from a file into an array of strings, where each entry represents a single line in the data file.
  2. Extract the first line (I assume that is what Array.remove(0) does) and split it into an array of semicolon-separated header entries.
  3. Create a map M : "header entry" -> "column index" for future use
  4. For each line in the remaining lines:
    1. Get the array of semicolon-separated entries (counter-intuitively, the array is called "rows")
    2. Get the index of the column with the header "Province", read the entry at this location, and store it in the variable "provinceName".
    3. Do the equivalent for municipalityName and municipalityAltitude (in the 2nd case also convert the string to an integer)
    4. (Do some other stuff in the part of the code that is not highlighted)
 

What is a CSV file?

A CSV (Comma Separated Values) file is a type of plain text file that stores tabular data, such as numbers and text, in a structured format. Each row in the file represents a record, and each column represents a field within that record. CSV files are commonly used to store and manipulate data in a spreadsheet or database.

How do I read a CSV file?

To read a CSV file, you can use a programming language or software that supports CSV file processing. For example, in Python, you can use the built-in csv module to read data from a CSV file. Many spreadsheet programs, such as Microsoft Excel, also have the ability to import and manipulate CSV data.

What can I do with the data from a CSV file?

Once you have read a CSV file, you can analyze and manipulate the data in various ways depending on your needs. You can perform calculations, create visualizations, and extract specific information from the data. The possibilities are endless, and it ultimately depends on the type of data and your research objectives.

What is data processing?

Data processing is the act of transforming raw data into a more meaningful and useful form. This can involve tasks such as cleaning and organizing the data, performing calculations or statistical analyses, and creating visualizations. The goal of data processing is to extract insights and information from the data that can be used for decision making or further analysis.

What are some common challenges when processing CSV data?

Some common challenges when processing CSV data include dealing with missing or incorrect values, handling large datasets, and ensuring data integrity. It is important to thoroughly clean and validate the data before using it for analysis to avoid any errors or bias in the results. Additionally, understanding the structure and format of the CSV file is crucial to properly process the data.

Similar threads

  • Programming and Computer Science
Replies
14
Views
4K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
28
Views
3K
  • Programming and Computer Science
Replies
33
Views
4K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
34
Views
2K
Replies
52
Views
4K
  • Programming and Computer Science
Replies
1
Views
8K
Back
Top