Quote All Fields in CSV Output

Add quotes to all columns in csv files

You could do the following (see the explanatory rem remarks):

@echo off
rem // Read CSV file that is provided as command line argument:
for /F "usebackq delims=" %%L in ("%~1") do (
rem // Store currently read line:
set "LINE=%%L"
rem /* Toggle delayed expansion to be able to write and read a variable in the same block of code
rem (without it reading a variable would return the value present before the whole block executes): */
setlocal EnableDelayedExpansion
rem /* Return the current line but enclose it within `""`
rem and replace every `,` by `","`;
rem this results in every comma -separated field to appear in between quotes: */
echo("!LINE:,=","!"
endlocal
)

This relies on the assumption that no field value contains commas on its own.

Writing to csv puts quotes around quotes and quotes around entire cell

This should do the trick:

with open('phrases.csv', 'w', encoding='utf-8', newline='') as f:
thewriter = csv.writer(f, quoting=csv.QUOTE_NONE, escapechar='\\')
for x in range(0,1):
print(matrix[x])
thewriter.writerow(matrix[x])

You need to specify that the writer shouldn't generate additional quotes (quoting=csv.QUOTE_NONE):

Dialect.quoting

Controls when quotes should be generated by the writer
and recognised by the reader. It can take on any of the QUOTE_*
constants (see section Module Contents) and defaults to QUOTE_MINIMAL.

And will probably need to specify escaprechar too:

Dialect.escapechar

A one-character string used by the writer to escape
the delimiter if quoting is set to QUOTE_NONE and the quotechar if
doublequote is False. On reading, the escapechar removes any special
meaning from the following character. It defaults to None, which
disables escaping.

Quote all fields in CSV output

Change

CSV::Writer.generate(@out)do |csv|

to

CSV::Writer.generate(@out, {:force_quotes=>true}) do |csv|

How to add double quote in csv file where field contains space?

Here is a simple awk:

$ awk 'BEGIN{FS=OFS=";"}{for(i=1;i<=NF;++i) if ($i ~ / /) $i = "\042" $i "\042"}1' file.csv

How to save CSV with all fields quoted?

tl;dr Enable quoteAll option.

scala> Seq(("hello", 5)).toDF.write.option("quoteAll", true).csv("hello5.csv")

The above gives the following output:

$ cat hello5.csv/part-00000-a0ecb4c2-76a9-4e08-9c54-6a7922376fe6-c000.csv
"hello","5"

That assumes the quote is " (see CSVOptions)

That however won't give you "Double quotes around all non-numeric characters." Sorry.

You can see all the options in CSVOptions that serves as the source of the options for the CSV reader and writer.

p.s. com.databricks.spark.csv is currently a mere alias for csv format. You can use both interchangeably, but the shorter csv is preferred.

p.s. Use option("header", false) (false as boolean not String) that will make your code slightly more type-safe.

Adding double quote delimiters into csv file

This is actually pretty easy in Excel (or any spreadsheet application).

You'll want to use the =CONCATENATE() function as shown in the formula bar in the following screenshot:

Step 1 involves adding quotes in column B,

Step 2 involves specifying the function and then copying it down column C (by now your spreadsheet should look like the screenshot),

Sample Image

Step 3 (if you need the text outside of the formula) involves copying column C, right-clicking on column D, choosing Paste Special >> Paste Values. Column D should then contain the text that was calculated in column C.



Related Topics



Leave a reply



Submit