简体   繁体   English

如何解析CSV(逗号分隔文件)并显示输出

[英]How do I parse through a CSV (comma separated file) and display output

Does anyone know how I would go about creating an application that will go through a comma separated file (CSV) and organize that information and subsequently put that into a database? 有谁知道我将如何创建一个应用程序,该应用程序将通过逗号分隔文件(CSV)进行组织,然后将该信息放入数据库中? For example, I have to go through a file that looks like this: 例如,我必须浏览一个看起来像这样的文件:

Name of Athlete, Birthday, Salary($)
John Morris, 9/25/1991, 5000000 
Mike Hawk, 2/2/1988, 1000000
Dewayne Johnson, 11/22/1985, 4000000

etc. 等等

How would I organize this information and set it up? 我将如何组织这些信息并进行设置? I am trying to make it so I can ask the user for input. 我正在努力做到这一点,所以我可以要求用户输入。 For example: have the user enter "a" for amount of salary and output all the players who make that EXACT amount of money. 例如:让用户输入“ a”作为薪水金额,并输出所有赚取该确切金额的玩家。

Player 1 name, Player 1 birthday Player 2 name, Player 2 birthday 玩家1的名字,玩家1的生日玩家2的名字,玩家2的生日

Any help is appreciated. 任何帮助表示赞赏。 I would appreciated help with reading the information and storing/retrieving it. 我会很乐意帮助您阅读信息并存储/检索它。

Don't give me the complete answer. 不要给我完整的答案。 I am a student learning this and I want to understand it. 我是一名学习此内容的学生,我想了解它。

I would think about these steps: 我会考虑以下步骤:

  • Create a class to map with the data in your CSV. 创建一个类以映射CSV中的数据。 For example, an Athlete class with attributes name , birthday and salary (it's up to you define each data type). 例如,一个具有namebirthdaysalary属性的Athlete类(由您定义每种数据类型)。
  • Read the CSV file. 读取CSV文件。 You can use Scanner or BufferedReader (using a FileReader ) to accomplish this. 您可以使用ScannerBufferedReader (使用FileReader )来完成此操作。
  • For each line read: 对于每一行,请阅读:
    • Split the string into several strings by comma (,) 用逗号(,)将字符串分成几个字符串
    • Create a new Athlete object and parse each string into an attribute to set it into the object. 创建一个新的Athlete对象,并将每个字符串解析为一个属性,以将其设置为对象。
    • Store the new Athlete in a collection ( List or something). 将新Athlete存储在集合中( List或类似内容)。
  • Once you completed loading a number of Athlete s, save them into your database. 完成加载多个Athlete ,将它们保存到数据库中。
  • Repeat this until you finish reading the csv. 重复此过程,直到完成阅读csv。

Splitting by a comma is a naive way of dealing with CSV. 用逗号分割是处理CSV的一种幼稚方式。 Unless you are 100% sure you will never have a comma in an actual field, then go this route, but if you may ever encounter a string like hello, my name is devshorts , you should use a CSV library. 除非您100%确定实际字段中永远不会有逗号,否则请hello, my name is devshorts此路线,但是,如果您可能遇到像hello, my name is devshorts这样的字符串hello, my name is devshorts ,则应该使用CSV库。 http://opencsv.sourceforge.net/ seems to be a good choice. http://opencsv.sourceforge.net/似乎是一个不错的选择。

Once thats done, map each entry to a class that represents what a line is. 完成后,将每个条目映射到代表一行的类。 Make an Atheltes class, where it has fields for Name , Birthdate , and Salary . 制作一个Atheltes类,其中包含NameBirthdateSalary字段。

You can use a direct JDBC connection to a sqlite/mysql/whatever database and insert (using raw SQL) the values into a table called Atheletes . 您可以使用与sqlite / mysql /任何数据库的直接JDBC连接,并将值插入(使用原始SQL)到称为Atheletes的表中。 If you want to use an ORM go with hibernate, but that may be more work than you need to set up. 如果要使用ORM,请与休眠一起使用,但这可能比您需要设置的工作还要多。

Well a simple way of reading this type of files would be using a BufferedReader that allows you to read the file line by line. 读取此类文件的一种简单方法是使用BufferedReader,它允许您逐行读取文件。

Then use split function to separate the string by delimiter and convert it into an array. 然后使用split函数通过定界符分隔字符串并将其转换为数组。

With the resulting array you can insert data into the database making proper conversion as required. 使用结果数组,您可以将数据插入数据库,以根据需要进行适当的转换。

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

相关问题 如何在Java中解析CSV(excel,不用逗号分隔)文件? - how can I parse CSV(excel,not separated by comma) file in Java ? 如何用逗号将逗号分隔的行(CSV)解析为某些项目? - How to parse a comma separated line (CSV) with some items in quotation marks? 如何从 CSV 输出中删除逗号? - How do I remove the comma from a CSV output? 如何在Java Eclipse中读取Excel CSV文件(逗号分隔值)? - How to read a excel CSV file (comma separated value) in Java Eclipse? 如何以逗号分隔的数组值显示/打印字符串? - How do I display/print a string in a comma-separated array value? 如何从命令行作为参数输入的文本文件中读取逗号分隔的字符串? - How do I read in comma separated string from text file that is inputted from command-line as argument? 如何在 spring 批处理中的单个项目阅读器中读取逗号分隔和 pipe 行分隔的 csv 文件 - how to read both comma separated and pipe line separated csv file in a single item reader in spring batch 如何在MySQL中使用Hibernate插入逗号分隔值? - How do I insert comma separated value using Hibernate with MySQL? 我如何使用DOM正确解析我的csv文件? - How do i correctly parse my csv file using DOM? 如何在此输出中添加空格或逗号? - How do I add space or comma in this output?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM