Write CSV with encoding ISO8859-1 return error

Hello

The problem is that I am supposed to support writing a CSV response with text encoding both as UTF-8 and ISO8859-1
This does not seem to work as planned.

When I try to stream:

… encoding: rune not supported by encoding.

To set up the csv-writer I use the following code:

// w is http.ResponseWriter
var conv io.Writer = w
if encoding == altEncoding {
    conv = charmap.ISO8859_1.NewEncoder().Writer(w)
}
csvWriter := csv.NewWriter(conv)
csvWriter.Comma = separator

As long as I use UTF-8, everything seems ok.

Any help is appreciated!

2 Likes

What are you trying to write? Not all characters in UTF-8 can be represented in ISO 8859-1.

2 Likes

Yes, that is awful truth about ISO8859-1 when converting from UTF-8 to ISO8859-1.
My data are names of persons and many of them are international, and as far as I am able to understand, those are the cause of this problem.

2 Likes

That’s indeed quite likely. ISO 8859-1 covers western european scripts. Outside of that you need a better character set.

2 Likes

Thank you very much, Sir!

Well, I guess, I have to go back to my client and say: “Nope, not possible!” :wink:

3 Likes

If its mostly names, you can try to ask the original data source for transcriptions or to create it computationally. But transcribing or dropping ISO8859-1 are probably the only options.

2 Likes

Yes, I will probably drop the whole thing.

Than you, Sir

2 Likes

Yes, I have that option too

Thank you, Sir!

2 Likes

By using inconv on the terminal rather than a library wrapping it, you are just defering the problem. Unicode knows more than a million “characters”, ISO encodings can encode just 256. You’ll always have a loss of information when not using UTF-8/16/32.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.