Search in sources :

Example 11 with CsvWriterSettings

use of com.univocity.parsers.csv.CsvWriterSettings in project tablesaw by jtablesaw.

the class CsvWriter method write.

public void write(Table table, CsvWriteOptions options) {
    CsvWriterSettings settings = createSettings(options);
    com.univocity.parsers.csv.CsvWriter csvWriter = null;
    // Creates a writer with the above settings;
    try {
        csvWriter = new com.univocity.parsers.csv.CsvWriter(options.destination().createWriter(), settings);
        writeHeader(table, options, csvWriter);
        for (int r = 0; r < table.rowCount(); r++) {
            String[] entries = new String[table.columnCount()];
            for (int c = 0; c < table.columnCount(); c++) {
                writeValues(table, options, r, entries, c);
            }
            csvWriter.writeRow(entries);
        }
    } finally {
        if (csvWriter != null) {
            csvWriter.flush();
            if (options.autoClose())
                csvWriter.close();
        }
    }
}
Also used : CsvWriterSettings(com.univocity.parsers.csv.CsvWriterSettings)

Example 12 with CsvWriterSettings

use of com.univocity.parsers.csv.CsvWriterSettings in project droid by digital-preservation.

the class CsvItemWriter method open.

@Override
public void open(final Writer writer) {
    final CsvWriterSettings csvWriterSettings = new CsvWriterSettings();
    csvWriterSettings.setQuoteAllFields(true);
    CsvFormat format = new CsvFormat();
    // following Unix convention on line separators as previously
    format.setLineSeparator("\n");
    csvWriterSettings.setFormat(format);
    csvWriter = new CsvWriter(writer, csvWriterSettings);
    if (headers == null) {
        headers = HEADERS;
    }
    csvWriter.writeHeaders(headers);
}
Also used : CsvWriter(com.univocity.parsers.csv.CsvWriter) CsvWriterSettings(com.univocity.parsers.csv.CsvWriterSettings) CsvFormat(com.univocity.parsers.csv.CsvFormat)

Example 13 with CsvWriterSettings

use of com.univocity.parsers.csv.CsvWriterSettings in project hillview by vmware.

the class CsvFileTest method csvWriterTest.

@Test
public void csvWriterTest() throws IOException {
    // The Csv writer (Univocity) we were using had a bug,
    // reproduced with this test.
    String[] data = new String[] { "", null };
    CsvWriterSettings settings = new CsvWriterSettings();
    CsvFormat format = new CsvFormat();
    settings.setFormat(format);
    settings.setEmptyValue("\"\"");
    settings.setNullValue("");
    String fileName = "tmp.csv";
    Writer fw = new FileWriter(fileName);
    CsvWriter writer = new CsvWriter(fw, settings);
    writer.writeRow(data);
    writer.close();
    fw.close();
    File file = new File(fileName);
    if (file.exists()) {
        @SuppressWarnings("unused") boolean ignored = file.delete();
    }
}
Also used : CsvWriter(com.univocity.parsers.csv.CsvWriter) FileWriter(java.io.FileWriter) CsvFileWriter(org.hillview.storage.CsvFileWriter) CsvWriterSettings(com.univocity.parsers.csv.CsvWriterSettings) CsvFormat(com.univocity.parsers.csv.CsvFormat) File(java.io.File) FileWriter(java.io.FileWriter) CsvWriter(com.univocity.parsers.csv.CsvWriter) CsvFileWriter(org.hillview.storage.CsvFileWriter) Writer(java.io.Writer) BaseTest(org.hillview.test.BaseTest) Test(org.junit.Test)

Example 14 with CsvWriterSettings

use of com.univocity.parsers.csv.CsvWriterSettings in project dsbulk by datastax.

the class CSVConnector method init.

@Override
public void init() throws URISyntaxException, IOException {
    super.init();
    CsvFormat format = new CsvFormat();
    format.setDelimiter(delimiter);
    format.setQuote(quote);
    format.setQuoteEscape(escape);
    format.setComment(comment);
    boolean autoNewline = AUTO_NEWLINE.equalsIgnoreCase(newline);
    if (read) {
        parserSettings = new CsvParserSettings();
        parserSettings.setFormat(format);
        parserSettings.setNullValue(AUTO.equalsIgnoreCase(nullValue) ? null : nullValue);
        parserSettings.setEmptyValue(AUTO.equalsIgnoreCase(emptyValue) ? "" : emptyValue);
        // do not use this feature as the parser throws an error if the file
        // has fewer lines than skipRecords;
        // we'll use the skip() operator instead.
        // parserSettings.setNumberOfRowsToSkip(skipRecords);
        parserSettings.setHeaderExtractionEnabled(header);
        parserSettings.setMaxCharsPerColumn(maxCharsPerColumn);
        parserSettings.setMaxColumns(maxColumns);
        parserSettings.setNormalizeLineEndingsWithinQuotes(normalizeLineEndingsInQuotes);
        parserSettings.setIgnoreLeadingWhitespaces(ignoreLeadingWhitespaces);
        parserSettings.setIgnoreTrailingWhitespaces(ignoreTrailingWhitespaces);
        parserSettings.setIgnoreLeadingWhitespacesInQuotes(ignoreLeadingWhitespacesInQuotes);
        parserSettings.setIgnoreTrailingWhitespacesInQuotes(ignoreTrailingWhitespacesInQuotes);
        if (autoNewline) {
            parserSettings.setLineSeparatorDetectionEnabled(true);
        } else {
            format.setLineSeparator(newline);
        }
    } else {
        writerSettings = new CsvWriterSettings();
        writerSettings.setFormat(format);
        writerSettings.setNullValue(AUTO.equalsIgnoreCase(nullValue) ? null : nullValue);
        // DAT-605: use empty quoted fields by default to distinguish empty strings from nulls
        writerSettings.setEmptyValue(AUTO.equalsIgnoreCase(emptyValue) ? "" + quote + quote : emptyValue);
        writerSettings.setQuoteEscapingEnabled(true);
        writerSettings.setIgnoreLeadingWhitespaces(ignoreLeadingWhitespaces);
        writerSettings.setIgnoreTrailingWhitespaces(ignoreTrailingWhitespaces);
        writerSettings.setMaxColumns(maxColumns);
        writerSettings.setNormalizeLineEndingsWithinQuotes(normalizeLineEndingsInQuotes);
        if (autoNewline) {
            format.setLineSeparator(System.lineSeparator());
        } else {
            format.setLineSeparator(newline);
        }
    }
}
Also used : CsvParserSettings(com.univocity.parsers.csv.CsvParserSettings) CsvFormat(com.univocity.parsers.csv.CsvFormat) CsvWriterSettings(com.univocity.parsers.csv.CsvWriterSettings)

Example 15 with CsvWriterSettings

use of com.univocity.parsers.csv.CsvWriterSettings in project drill by apache.

the class TextRecordWriter method init.

@Override
public void init(Map<String, String> writerOptions) throws IOException {
    this.location = writerOptions.get("location");
    this.prefix = writerOptions.get("prefix");
    this.fs = FileSystem.get(fsConf);
    String extension = writerOptions.get("extension");
    this.extension = extension == null ? "" : "." + extension;
    this.fileNumberIndex = 0;
    CsvWriterSettings writerSettings = new CsvWriterSettings();
    writerSettings.setMaxColumns(TextFormatPlugin.MAXIMUM_NUMBER_COLUMNS);
    writerSettings.setMaxCharsPerColumn(TextFormatPlugin.MAX_CHARS_PER_COLUMN);
    writerSettings.setHeaderWritingEnabled(Boolean.parseBoolean(writerOptions.get("addHeader")));
    writerSettings.setQuoteAllFields(Boolean.parseBoolean(writerOptions.get("forceQuotes")));
    CsvFormat format = writerSettings.getFormat();
    format.setLineSeparator(writerOptions.get("lineSeparator"));
    format.setDelimiter(writerOptions.get("fieldDelimiter"));
    format.setQuote(writerOptions.get("quote").charAt(0));
    format.setQuoteEscape(writerOptions.get("escape").charAt(0));
    // do not escape "escape" char
    format.setCharToEscapeQuoteEscaping(TextFormatPlugin.NULL_CHAR);
    this.writerSettings = writerSettings;
    logger.trace("Text writer settings: {}", this.writerSettings);
}
Also used : CsvWriterSettings(com.univocity.parsers.csv.CsvWriterSettings) CsvFormat(com.univocity.parsers.csv.CsvFormat)

Aggregations

CsvWriterSettings (com.univocity.parsers.csv.CsvWriterSettings)16 CsvWriter (com.univocity.parsers.csv.CsvWriter)7 CsvFormat (com.univocity.parsers.csv.CsvFormat)5 CsvParserSettings (com.univocity.parsers.csv.CsvParserSettings)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 Writer (java.io.Writer)2 PolicyStats (com.github.benmanes.caffeine.cache.simulator.policy.PolicyStats)1 LEGACY_TEXT (com.powsybl.psse.model.io.FileFormat.LEGACY_TEXT)1 BeanWriterProcessor (com.univocity.parsers.common.processor.BeanWriterProcessor)1 CsvParser (com.univocity.parsers.csv.CsvParser)1 java.awt (java.awt)1 BufferedImage (java.awt.image.BufferedImage)1 FileWriter (java.io.FileWriter)1 OutputStreamWriter (java.io.OutputStreamWriter)1 StringWriter (java.io.StringWriter)1 StandardCharsets (java.nio.charset.StandardCharsets)1 DecimalFormat (java.text.DecimalFormat)1 java.util (java.util)1 List (java.util.List)1