1

I'm using this library to convert a block to a CSV. However, when it encounters a string with a comma in it it molds that string. Normally not a problem except that the curly-braces seem to confuse Excel.

So, {This, is a test} gets turned into | {this | is a test} | (each side of the comma is put into separate cells).

At first I thought I needed to escape the comma but it turns out what I need to do is turn the curly braces into quotes. Is there a quick or REBOL-recommended way to do this?

Darrell Brogdon
  • 6,843
  • 9
  • 47
  • 62

2 Answers2

4

The purpose of 'MOLD in %csv.r is to wrap values containing commas into double quotes.

But unfortunately 'MOLD puts strings longer than 50 characters into curly braces instead of double quotes, for better readability.

I don't know how to affect this behaviour, so I would just replace 'MOLD in Item: mold Item and Heading: mold Heading with 'DBL-QUOTE, which would simply be defined as

dbl-quote: func[s][rejoin [{"} s {"}]]
onetom
  • 199
  • 8
2

Use csv-tools.r instead. It has that functionality built in, and is verified to be Excel compatible. It will work with Rebol 2 and 3, and has been in production use for years.

BrianH
  • 2,186
  • 16
  • 14
  • The to-csv function in csv-tools.r seems to have problems with nested blocks like `[["one" "two" "three"] ["two" "two" "three"]]`. – Darrell Brogdon Aug 23 '13 at 23:03
  • The `to-csv` function is a single-line converter, like the documentation in the file says. Single lines of CSV don't have nested blocks, they are conceptually series of individual values. Use the `map-each` function with `to-csv` to generate a block of lines, then `write` to write them out to a file. – BrianH Aug 27 '13 at 22:19