Writing to CSV
Hello,
I'm using CsvWriter to write a .csv file. It is very easy to write, but I was confused a bit on my setup.
My CSV header is
String[] header = {"PRODUCT_NAME", "PHONE", "ADDRESS", "BIZURL", "HOURS", "CATEGORY", "SPEC", "SERVICES", "YEAR", "CONTACT", "REVIEW_CONTENT"};
There are 2 scrapeable files in my session:
1st get the following info:
DATARECORD:
- PRODUCT_NAME
- PHONE
- ADDRESS
- BIZURL
- PHONE
- ADDRESS
- BIZURL
and the 2nd:
DATARECORD:
- HOURS
- CATEGORY
- SPEC
- SERVICES
- YEAR
- CONTACT
- CATEGORY
- SPEC
- SERVICES
- YEAR
- CONTACT
and not DATARECORD (usual Main variable)
REVIEW_CONTENT
if I set to write after each pattern match for each extractor pattern it writes data from new line. So it obvious that i should join all the strings first and then write. Is it good practice or not?
Can you suggest something here, which code should I use?
Cheers
tellieno, You are correct.
tellieno,
You are correct. It is best to store up the data for a given line in your CSV and then write it to a file in one shot. To do so, you have a few options.
Once all data for the current iteration is saved in session and you're ready to write it out to a file then you will first need to add the data to a dataRecord object that the CsvWriter then uses to write to a file.
If you chose to store your extracted data as dataSets then you'll be working with three session variables: Two dataSet objects and one variable.
Here is example code for Iterating over DataSets & DataRecords. When creating a new dataSet object to store your soon-to-be-written dataRecord object just use the two dataSets you already have in stored as session variables.
Otherwise, if you stored each variable individually, just add them one-by-one to your dataRecord.
Once everything is in one dataSet with one dataRecord then have the CsvWriter Write the dataRecord to the file.
-Scott