简体   繁体   English

如何计算csv文件的总人数?

[英]How to count total number of persons a csv file?

given the following code:给出以下代码:

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * siehe Zettel 0 - Aufgabe 2
 */
public class PrintCSV_Task2 {

    private static String FILENAME = "data.csv";

    public static void main(String[] args) {
        System.out.println("---(Ausgabe) Aufgabe 2.1---");
        printAll_CSV_NUM(ReadCSV_Task2.fetchAll_CSV_NUM(FILENAME));
        System.out.println("---(Ausgabe) Aufgabe 2.2---");
        printAll_CSV_ASSOC(ReadCSV_Task2.fetchAll_CSV_ASSOC(FILENAME));

        System.out.println("Compiled, no syntactical failures =)");
    }

    /**
     * Aufgabe 2.1
     * @param csvData die Daten der CSV
     */
    public static void printAll_CSV_NUM(List<String[]> csvData) {
        csvData.stream().map(entry -> Arrays.stream(entry).reduce(
                "", //init
                (e1, e2) -> (e1.equals("")) ? e2 : (e1 + "; " + e2)) //concat
        ).forEach(System.out::println);
    }

    /**
     * Aufgabe 2.2
     * @param csvData die Daten der CSV
     */
    public static void printAll_CSV_ASSOC(List<Map<String, String>> csvData) {
        csvData.stream().map(entry -> entry.entrySet().stream().reduce(
                new StringBuilder(), //init
                (s, e) -> s.append(s.length() == 0 ? "" : "; ").append(e.getKey()).append(':') .append(e.getValue()), //concat String with MapEntry
                (s1, s2) ->  s1.append(s1.length() == 0 ? "" : "; ").append(s2) //concat already converted MapEntry_ies (String) with another
        )).forEach(System.out::println);
    }
}

and:和:

import java.util.List;
import java.util.Map;

/**
 * siehe Zettel 0 - Aufgabe 3
 */
public class ProcessCSV_Task2 {
    private static String FILENAME = "data.csv";

    public static void main(String[] args) {
        System.out.println("---(Ausgabe) Aufgabe 2.1---");
        System.out.printf("Die Anzahl der Personen ist %d", countCsvData_CSV_NUM(ReadCSV_Task2.fetchAll_CSV_NUM(FILENAME)));
        System.out.println();
        System.out.println("---(Ausgabe) Aufgabe 2.2---");
        PrintCSV_Task2.printAll_CSV_NUM(selectCsvDataByYear_CSV_NUM(ReadCSV_Task2.fetchAll_CSV_NUM(FILENAME), 1988));
        System.out.println("---(Ausgabe) Aufgabe 2.3---");
        PrintCSV_Task2.printAll_CSV_ASSOC(selectCsvDataByValue_CSV_ASSOC(ReadCSV_Task2.fetchAll_CSV_ASSOC(FILENAME), "Stadt", "Paderklar"));
        System.out.println("---(Ausgabe) Aufgabe 2.4---");
        System.out.printf("Das Durchschnittsgeburtsjahr der Personen in %s ist %f", "Paderklar", avgCsvDataByTown_CSV_ASSOC(ReadCSV_Task2.fetchAll_CSV_ASSOC(FILENAME), "Paderklar"));
        System.out.println();

        System.out.println("Compiled, no syntactical failures =)");
    }

    /**
     * Aufgabe 3.1
     * @param csvData die Daten der CSV
     * @return die Anzahl der Personen
     */
    public static int countCsvData_CSV_NUM(List<String[]> csvData) {
        
    }

    /**
     * Aufgabe 2.2
     * @param csvData die Daten der CSV
     * @param selectedYear das ausgewählte Jahr, nach der die Daten gefiltert werden sollen
     * @return die Selektion
     */
    public static List<String[]> selectCsvDataByYear_CSV_NUM(List<String[]> csvData, int selectedYear) {

    }

    /**
     * Aufgabe 2.3
     * @param csvData die Daten der CSV
     * @param selectedKey das Attribut, nach dem selektiert werden sollst
     * @param selectedValue der bestimmte Attributwert
     * @return die Selektion
     */
    public static List<Map<String, String>> selectCsvDataByValue_CSV_ASSOC(List<Map<String, String>> csvData, String selectedKey, String selectedValue) {

    }

    /**
     * Aufgabe 2.4
     * @param csvData die Daten der CSV
     * @param selectedTown die ausgewählte Stadt
     * @return Durchschnittsswert der Geburtenjahre
     */
    public static double avgCsvDataByTown_CSV_ASSOC(List<Map<String, String>> csvData, String selectedTown) {

    }
}

How can i count the total number of persons in the data.csv file?如何计算 data.csv 文件中的总人数?

The data.csv file is an excel file and looks like this: data.csv 文件是一个 excel 文件,如下所示:

PersonID Name        Geburtsjahr  Stadt
0        Hans Müller 1988         Köln

(It contains 1000 Entries) (它包含 1000 个条目)

The function for counting is计数函数是

public static int countCsvData_CSV_NUM(List<String[]> csvData)

The data.csv file is an excel file and looks like this: data.csv 文件是一个 excel 文件,如下所示:

No it isn't.不,不是。 An excel file ends in .xls or .xlsx and doesn't look like that, it looks like meaningless gobbledygook (it is a binary format).一个 excel 文件以.xls.xlsx结尾,看起来不像那样,它看起来像毫无意义的 gobbledygook(它是一种二进制格式)。

Its a csv file: "Character Separated Values".它是一个 csv 文件:“字符分隔值”。 And the character separating them is, hopefully, a tab character.希望分隔它们的字符是制表符。 THere is a different style of data format called a fixed-length-record file, where each record consists of an exact and equal number of spaces.有一种不同风格的数据格式称为固定长度记录文件,其中每条记录由精确且相等数量的空格组成。 So, if the bytes in that file involve a 0 , and then a bunch of spaces, and then Hans Müller , then it's not a csv file at all, and you should rename it.因此,如果该文件中的字节涉及0 ,然后是一堆空格,然后是Hans Müller ,则它根本不是 csv 文件,您应该重命名它。 If that file contains a 0, then a tab character, and then Hans Müller , great, it's a CSV file (and the character used to separate, is a tab).如果该文件包含 0,然后是制表符,然后是Hans Müller ,太好了,它是一个 CSV 文件(用于分隔的字符是制表符)。

Given that there are 9 spaces between 1988 and Köln, you either have an extremely exotic tab stop configured, or it's a fixed-length-records file and not a CSV (nor an excel file), or you've taken some liberties when pasting it.鉴于 1988 年和科隆之间有 9 个空格,您要么配置了一个非常奇特的制表位,要么它是一个固定长度的记录文件而不是 CSV(也不是一个 excel 文件),或者您在粘贴时采取了一些自由它。

You need to figure out what the file format actually is before you can start writing code to read it.在开始编写代码来读取它之前,您需要弄清楚文件格式实际上是什么。

NB: The rest of this answer assumes it is, after all, tab-separated values.注意:这个答案的其余部分毕竟假设它是制表符分隔的值。 If it turns out to be fixed-length-records, stop reading here.如果结果是固定长度的记录,请在此处停止阅读。

A problem with CSV files is that there is no specification. CSV 文件的一个问题是没有规范。 Some CSV files allow records that have newlines in them (or tabs in them) by employing, for example, quotes, and some quote escape mechanism.某些 CSV 文件允许使用例如引号和某些引号转义机制在其中包含换行符(或其中的制表符)的记录。 Others do not.其他人没有。

Your paste doesn't include any 'interesting' data (where the data itself contains either tabs or newlines).您的粘贴不包含任何“有趣”的数据(其中数据本身包含制表符或换行符)。 Thus, it is impossible to tell.因此,无法判断。

Let's assume (I'll take 'Aufgabe' as a hint here) that it's homework and the course material has wished away the complications of CSV and you are just to assume the input data will not ever include tabs or newlines.让我们假设(我将在此处使用“Aufgabe”作为提示)这是家庭作业,并且课程材料希望消除 CSV 的复杂性,并且您只是假设输入数据永远不会包含制表符或换行符。

Then, your question boils down to an extremely simple: How many lines are in this file?那么,你的问题归结为一个非常简单的问题:这个文件有多少行?

However, the input to countCsvData_CSV_NUM (which, by the way, is not standard java, 'we' don't name methods that way, if you are in a position to reconsider the course material or venue of education you may want to consider it, this is.. not a good start to a first-steps java course) is a 'list of string arrays' which is rather bizarre.但是, countCsvData_CSV_NUM的输入(顺便说一下,这不是标准的 Java,“我们”不会以这种方式命名方法,如果您能够重新考虑课程材料或教育场所,您可能需要考虑它,这是 .. 第一步 Java 课程的良好开端)是一个相当奇怪的“字符串数组列表”。 Presumably the idea is that it is a list of each individual 'string', so then it's just.. return list.size();大概的想法是它是每个单独的“字符串”的列表,所以它只是.. return list.size(); . . I suggest you learn how to write java, and learn how to debug.我建议你学习如何编写java,并学习如何调试。 For example, write some code that just prints out, given some sample input, what that List<String[]> csvData actually contains.例如,编写一些代码,在给定一些示例输入的情况下打印出List<String[]> csvData实际包含的内容。 You can't really write code properly until you understand what you're supposed to do and what your inputs actually are, after all.毕竟,在您了解您应该做什么以及您的输入实际上是什么之前,您无法真正正确地编写代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM