how to rewrite csv file in java without double quotes - java

original csv
reformatted csv
i have crawled data that i want from web and saved them as CSV as above.
However, when i tried to exclude the data that i do not want that to be included on my data set and tried to save it again, i get all those unwanted quotation mark(like """) through all columns on my data
How do i fix this problem??
(I want to make all the column names to have same format as above to have just "column name"(one quotation mark))
BufferedReader br = Files.newBufferedReader(Paths.get("FilePath"));
CSVWriter cw = new CSVWriter(new OutputStreamWriter(new FileOutputStream("FileName.csv", true), StandardCharsets.UTF_8));
String val="";
while((val=br.readLine())!=null){
if(!val.contains("some keywords")){
continue;
}
cw.writeNext(new String[]{val});
}
cw.flush();

Related

How to create new column from CSVWriter in Java

I have a problem. I want to create a new CSV file from CSVWriter and my whole Array is set into the one cell.
This is my java code:
StringWriter s = new StringWriter();
#SuppressWarnings("deprecation")
CSVWriter writer = new CSVWriter(s, '\t');
String[] entries = new String[3];
entries[0] = "Test";
entries[1] = "Test";
writer.writeNext(entries);
writer.close();
String finalString = s.toString();
System.out.println(finalString);
I get the output like this: "first" "second" "third"
and my CSV is :
but I want to be like this:
Your code is OK, the problem is you open the CSV file by Excel with default settings.
If you open the CSV file with Notepad, it will look like this:
"Test" "Test"
And if you still want to open it with Excel, you are supposed to open it by following steps:
Create a new sheet.
Select Data > From Text File.
Select file (write.csv) to be imported.
Click Finish.

CSVPrinter datas appears not separated in columns

I'm trying to use this library commons-csv (1.5) for generating the csv file, i've make a simple program for seeing the result :
String SAMPLE_CSV_FILE = "./sample.csv";
try (
BufferedWriter writer = Files.newBufferedWriter(Paths.get(SAMPLE_CSV_FILE));
CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.EXCEL
.withHeader("ID", "Name", "Designation", "Company"));
) {
csvPrinter.printRecord("1", "Name1 ", "CEO", "Google");
csvPrinter.printRecord("2", "Name2", "CEO", "Microsoft");
csvPrinter.flush();
}
My CSV is well generated but the header and the data isnt separated in each column, When I open the CSV file, I see all the data and header in the first column
I dont found yet the good CSVFormat, how to separate them ?
The selected answer does not really show how you can make sure that comma is set as the delimiter.
To tell excel that comma should be used as a delimiter, Print printer.printRecord("SEP=,"); as the first record in your file. its a command that excel understands and it will not show in your file output.
try (CSVPrinter printer = new CSVPrinter(new FileWriter("file.csv"),CSVFormat.EXCEL)) {
printer.printRecord("SEP=,"); //this line does the margic.
printer.printRecord("Column A","Column B","Column C");
} catch (IOException ex) {
}
When I run your program, I get all the columns separated by comma's as expected. If you're using Excel to open the file, make sure you have selected the comma as the delimiter instead of the tab.

Split xml file in to multiple files using buffWrite or FileWriter

I have an xml file that has different types of medical data on it. I am trying to split the xml file in to multiple files by tag name or by row. I am getting this xml data from a csv file. The xml file data looks like this.
Xml File Data
CSV file that Im getting data from
Each file once split should be named descriptor_pdx01.xml, descriptor_pdx02.xml, descriptor_pdx03.xml ...... etc.
I am trying to accomplish this in Java using Buff Write or FileWriter. This is the code I wrote to write to the xml file. The problem is trying to figure out how to split the XML file in to multiple files in java using buffwrite and Filewriter
This is the content of the CSV FIle:
TESTCLIENT_2017_09_30 pdx01 Carcinosarcoma C34448 M 12
TESTCLIENT_2017_09_30 pdx02 Esophageal carcinoma C4025
TESTCLIENT_2017_09_30 pdx03 Esophageal carcinoma C4025 45
TESTCLIENT_2017_09_30 pdx04 Carcinosarcoma C34448 F
TESTCLIENT_2017_09_30 pdx05 Esophageal carcinoma C4025 F 60
TESTCLIENT_2017_09_30 pdx06 Esophageal carcinoma C4025 F 66
TESTCLIENT_2017_09_30 pdx07 Carcinosarcoma C34448 M 70
public static void writeXmlFile(String[] headers, ArrayList <String> stringsFromCsv, String fileName){
try {
BufferedWriter buffWrite = new BufferedWriter(new FileWriter(fileName));
buffWrite.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n");
buffWrite.write("<patient>\r\n");
//String array that is the same size is that of the string from csv from line 66-69
//For each string in the csv file
for(String s:stringsFromCsv){
buffWrite.write("\t<person>\r\n");
//Split the line into an array of strings
String fields[] = s.split(",");
String[] stringToWrite = fields;
//Initiate a string variable and using the field array values form the xml and assign it to the string variable
//DO a method call and Send the string and fileName (descriptor_field[1])
//For each item in that array of strings
for(int i=0; i<fields.length; i++){
//Define a String and keep on adding to that string using field element array, String should be outside for loopLol
//Write the corresponding header to the file, as well as the value from the array 'fields'
buffWrite.write("\t\t<" + headers[i] +">"+ fields[i] + "</" + headers[i] +">\n");
}
buffWrite.write("\t</person>\n");
}
buffWrite.write("</people>");
buffWrite.close();
}
catch (IOException ioe){ System.err.println("Error while writing to xml file in writeXmlFile: "); ioe.printStackTrace();
}
}
If you could offer any guidance on how to do this it would be greatly appreciated.
There are a lot of issues in your code as some people commented in your question.
I would use a lightweight library to handle both your CSV and XML files into a POJO.
My suggestion? BeanIO is a very good library to read/write CSV and XML files.
Write your POJO and use BeanIO's #Record and #Field annotations. BeanIO will gracefully parse both CSV / XML into your POJO's so you can do whatever you need.

Japanese character not showing properly converting CSV file

I am converting CSV file from Tatoeba project. It contains Japanese characters. I am inserting data into SQLite database. Insertion is going without a problem, but characters are showing not properly.
If I insert directly:
String str = content_parts[2];
sentence.setValue(str);
Getting values like this:
ãã¿ã«ã¡ãã£ã¨ãããã®ããã£ã¦ãããã
I have tried to decode to UTF8 from JIS:
String str = content_parts[2];
byte[] utf8EncodedBytes = str.getBytes("JIS");
String s = new String(utf8EncodedBytes, "UTF-8");
sentence.setValue(s);
JIS:
$B!)!)!)!)!)!)!)!)!)!)!)!)!)!)!)!)!)!r!)!)!/!)!)!)!)!)!)!)!)!)!)!)!)!)!)!)!)!)!)!)!)!r!)!)!)!)!)!)!)!)!)!)!)!)!)!)!)(B
Shift-JIS:
????\??????�N?�}??????????????????��?????�N?�N???��??????
Shift_JIS:
????\????????????????????????��?�N??????????????????��??????
CSV file (when opened by Excel 2010)
n きみにちょっとしたものをもってきたよ。
What I am doing wrong? How to solve this problem?
If you are still searching for solution, refer below link
setting-a-utf-8-in-java-and-csv-file and handle Japanese characters
csv-reports-not-displaying-japanese-characters
In brief, add BOM(byte order mark) characters to your file outputstream before passing it to outputstream writer.
String content="some string to write in file(in any language)";
FileOutputStream fos = new FileOutputStream("D:\csvFile.csv");
fos.write(239);
fos.write(187);
fos.write(191);
Writer w = new BufferedWriter(new OutputStreamWriter(fos, StandardCharsets.UTF_8));
w.write(content);
w.close();
Hope this will help

Delete the content of csv file in Java

Every day I update a csv file using a FileWriter. When we step into a new month I have to delete the data from the previous month. My below code only updates a data in a csv file so, please help in deleting the previous month's data.
At least I need to know how to delete the data in csv file using FileWriter, so that I can manage to code for deleting previous month data.
private static void eventsUpdate(HttpServletRequest request,
HttpServletResponse response) {
String date=request.getParameter("date"); //getting from jsp page
String event=request.getParameter("event"); //getting from jsp page
File file = new File( "D:///events/events.csv");
if ( !file.exists() )
file.createNewFile();
FileWriter fw = new FileWriter(file,true);
BufferedWriter writer = new BufferedWriter( fw );
writer.write(date);
writer.write(",");
writer.write(event);
System.out.println("writing into excel");
writer.newLine();
writer.close();
fw.close();
}
Actually when you use the true argument in your FileWriter instantiation, you create a file writer object in append mode.
FileWriter fw = new FileWriter(file,false);
If you don't use the true option, the file will be overwritten by the new content. Therefore, I would suggest to follow this roadmap:
Read the file content as a whole, in a String
Remove the content you want to remove from the specific String
Overwrite the file
content using your new content in the specific String
Hope I helped!
I suggest you that you may create unique name file like march2014, january2013 instead of deleting it. Every month you need to read your target file which is named by your month strategy.
For ex:
When you reach april2014, create a new file april2014 etc. and read it for that month.

Categories

Resources