Parse through text files and write into CSV
I have about 100 different text files in the same format. Each file has observations about certain events at certain time periods for a particular person. I am trying to parse out the relevant information for a few individuals for each of the different text files. Ideally, I want to get parse through all this information and create a CSV file (eventually to be imported into Excel) with all the information I need from ALL the text files. Any help/ideas would be greatly appreciated. I would prefer to use java...but any simpler methods are appreciated.
The log files are structured as below: changed data to preserve private information
|||ID||NAME||TIME||开发者_开发知识库FIRSTMEASURE^DATA||SECONDMEASURE^DATA|| etc...
TIME appears like 20110825111501 for 2011, 08/25 11:15:01 AM
Here are the steps in Java:
- Open the file using FileReader
- You could also wrapped the FileReader with BufferedReader and use readLine() method to read the file line by line
- For each line you need to parse it. You know best the data definition of each line, to help you might be able to use various String functions or Java Regex
- You could do the same thing for the Date. Check if you could utilize DateFormat
- Once you parse the data, you could start building your CSV File using CSVParser mentioned above or write it your own using FileOutputStream
- When you are ready to to convert to Excel, you could use Apache POI for Excel
Let me know if you need further clarification
Just to parse through the text file and use CSVParser from apache to write to a csv file. Additionally if you want to write in to excel, you can simply use Apache POI or JXL for that.
You can use SuperCSV to parse the file into a bean and also to create a csv-file.
精彩评论