Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve CSV output formatting to include quotation characters #33

Closed
elvarb opened this issue Jan 11, 2019 · 11 comments
Closed

Improve CSV output formatting to include quotation characters #33

elvarb opened this issue Jan 11, 2019 · 11 comments

Comments

@elvarb
Copy link

elvarb commented Jan 11, 2019

When for example you have a source file like this

"col1","col2" "dat1","dat2"

And output it as -ocsv the results are

col1,col2 dat1,dat2

You can right now configure the separator character with -od
A similar one for quotes could be added

@noborus
Copy link
Owner

noborus commented Jan 12, 2019

As you said, the output of CSV is not flexible.

The encoding/csv is used for CSV output.
https://golang.org/pkg/encoding/csv/

In this package, it is output in double quotation only when necessary.

echo 'a,b,c"d'|trdsql -ocsv "SELECT * FROM -" 
a,b,"c""d"

If you really need it, I would like to be added to golang package CSV.

@noborus
Copy link
Owner

noborus commented Jun 23, 2019

I just released v0.6.1.
It can be used as a library.
You may be able to resolve this issue.
With the following code:

package main

import (
	"io"
	"log"
	"os"
	"strconv"
	"strings"

	"github.com/noborus/trdsql"
)

type QuoteWrite struct {
	writer io.Writer
}

func NewQuoteCSVWriter(w io.Writer) *QuoteWrite {
	return &QuoteWrite{
		writer: w,
	}
}

func (w *QuoteWrite) PreWrite(columns []string, types []string) error {
	return nil
}

func (w *QuoteWrite) WriteRow(values []interface{}, columns []string) error {
	qColumns := make([]string, len(values))
	for i, col := range values {
		qColumns[i] = strconv.Quote(trdsql.ValString(col))
	}
	_, err := w.writer.Write([]byte(strings.Join(qColumns, ",") + "\n"))
	return err
}
func (w *QuoteWrite) PostWrite() error {
	return nil
}

func main() {
	trd := trdsql.NewTRDSQL(
		trdsql.NewImporter(),
		trdsql.NewExporter(NewQuoteCSVWriter(os.Stdout)),
	)
	err := trd.Exec("SELECT * FROM testdata/test.csv")
	if err != nil {
		log.Fatal(err)
	}
}

@awrog
Copy link

awrog commented Jan 22, 2020

I really love to use trdsql, awful speed and possibilities!
In processing large log-files the lack of being able to generate output with quotes is bugging me

Here you state:

You can right now configure the separator character with -od
A similar one for quotes could be added

Could you please add this to possible -od output options?

@noborus
Copy link
Owner

noborus commented Jan 22, 2020

Thank you for your opinion.
Force quotes are not supported by the standard CSV module.
So, before adding the option, consider using another CSV writer or implementing one.

@awrog
Copy link

awrog commented Jan 23, 2020

So, before adding the option, consider using another CSV writer or implementing one.

It concerns the activitylog from Twillio SendGrid, so, no chance of getting it changed.

@noborus
Copy link
Owner

noborus commented Jan 23, 2020

Sorry. Change the 'encoding/csv' part.
For example, you can change https://github.com/tushar2708/altcsv to use.

I haven't decided yet what to do, but I'm considering changing it to not use encoding/csv.

diff --git a/output_csv.go b/output_csv.go
index 7b7b345..da337bd 100644
--- a/output_csv.go
+++ b/output_csv.go
@@ -1,12 +1,12 @@
 package trdsql
 
 import (
-       "encoding/csv"
+       "github.com/tushar2708/altcsv"
 )
 
 // CSVWriter provides methods of the Writer interface.
 type CSVWriter struct {
-       writer    *csv.Writer
+       writer    *altcsv.Writer
        results   []string
        outHeader bool
 }
@@ -15,8 +15,9 @@ type CSVWriter struct {
 func NewCSVWriter(writeOpts *WriteOpts) *CSVWriter {
        var err error
        w := &CSVWriter{}
-       w.writer = csv.NewWriter(writeOpts.OutStream)
+       w.writer = altcsv.NewWriter(writeOpts.OutStream)
        w.writer.Comma, err = delimiter(writeOpts.OutDelimiter)
+       w.writer.AllQuotes = true
        if err != nil {
                debug.Printf("%s\n", err)
        }

noborus added a commit that referenced this issue Jan 27, 2020
Remove use of encoding/csv, and output CSV in writer.
Added option to specify quote character.
Option to set CRLF at the end of line.
CSV output performance has been improved.
@noborus
Copy link
Owner

noborus commented Jan 27, 2020

I added a new option to address this issue (#118) .
I want your opinion on this option.
If the name or description of the added option is not appropriate, please indicate it.
Added are - oq and - oaq and - ocrlf.

@awrog
Copy link

awrog commented Jan 28, 2020

Looks fine to me.
I'll wait for a new release to test it.

@noborus
Copy link
Owner

noborus commented Jan 28, 2020

Looks fine to me.
I'll wait for a new release to test it.

Thank you very much.

noborus added a commit that referenced this issue Jan 30, 2020
Added option to quote all columns with quotes(#33)
@noborus
Copy link
Owner

noborus commented Feb 4, 2020

I just released version 0.7.4 for this issue.
Thank you.

@noborus noborus closed this as completed Feb 4, 2020
@awrog
Copy link

awrog commented Feb 4, 2020

Works great!! Thanx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants