Add double quotes in csv file


















Hey, Scripting Guy! However, I need to put double quote marks around some of the items in that file. How do I do that? A line of code like this is bound to end in failure:. Well, like we said, double quotes indicate the beginning and ending of strings.

In other words, the string consists of everything embedded inside the first set of double quotes. However, you have some additional text following that string: 1. As a result, it simply throws up its hands in despair, and the script fails. To get around this we need to find a different way to specify that double quotes should be written to the text file.

If we want to insert double quotes into a file we can do this by specifying Chr 34 ; the Chr function takes the ASCII value — 34 — and converts it to an actual character in this case double quotes. That brings us to the line of code that writes the desired line to the file:. Note the space after the equal sign.

Notice the extra doublequotes at the beginning and the end, which clearly should not be there. If I render a literal control with those extra double quotes the functionality breaks. But it might be difficult to get what you want if you don't control Excel exporting, and you don't control the importing program.

You might have to tweak the text file in the middle of the process. This is well documented and is actually expected behaviour. However, getting round it seems tricky, and I can think of only work arounds. The work around appears to be open the exported file and Find and Replace the quote marks with nothing to remove the quote marks.

If you need more control eg, you may want the quote marks in some occaisons then you have to do it manually, or hack it - add a unique keyword where you want the " to exist such as not using " but instead qwertquote since this string is going to be unique you can find and replace it with a quote mark or, write a utility app to do it for you since you have programming experience.

This macro will output a text file without surrounding cells which have commas in quotation marks, or doubling quotation marks in the text:.

Each of the embedded double-quote characters must be represented by a pair of double-quote characters. So a csv file needs those double quotes to be escaped by using another set of double quotes , because the double quote by itself denotes the boundaries of a field.

Sign up to join this community. The best answers are voted up and rise to the top. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Learn more. Excel adds double quotes on csv export [duplicate] Ask Question. Asked 6 years, 11 months ago. Active 6 years, 11 months ago. There are much better tools available.

For example, if you sudo apt install csvtool or equivalent on your favourite distro, you can use its call-per-line functionality to process each line in the input file. See the following script for an example:. Note the quotify function which is called for each line in the CSV file, with the arguments set to each field within that line sans quotes, whether the original fields had quotes or not. It basically constructs a string of all the fields in the line, with quotes around them, then writes that to standard output, as shown below in the output from that script:.

Even though using a separate tool is probably the easiest way to go, if you absolutely cannot install other packages, then you're going to have to code up something in a package you already have. The following bash script is a good place to start, as it uses no other tools to achieve its goal. But, with that in mind, here we go. I'll offer a brief textual description of each section but hopefully the comments in the code will be enough to figure out what's going on.

First, a function for finding the position if some string within another string, useful for working out the field bounds:. Then we can use that in the function that works out the length of the next field. This basically just looks for the next comma for unquoted fields, and does special handling for quoted fields by building up the field from segments it has to handle quotes within quotes and commas :.

And, on the off-chance you want to read directly from a file though providing a file name that's empty or "-" will use standard input so you can probably just use the file-based function for everything :. And, finally, because every program that's not a "Hello, world" one deserves some form of test harness, this is what you can use to test the various capabilities:.

And, since a test harness is of little use unless you run the tests, here's the results of the first run:. Hopefully, that will be enough to get you going in the absence of being able to install packages. Of course, if one of the packages you can't install in bash itself, then you have problems that I can't help you with How are we doing? Please help us improve Stack Overflow. Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams?

Collectives on Stack Overflow.



0コメント

  • 1000 / 1000