0

I have been trying to read utf-16 little endian encoded csv file but I was getting error (undefined: utf16.NewReader) and could not figure out any solution so far.

Is there any better way to Read UTF-16 Little Endian encoded csv file using Golang?

The code I used,

package main

import (
    "bufio"
    "encoding/csv"
    "os"
    "unicode/utf16"
)

func main() {
    // Open the CSV file
    file, err := os.Open("myfile.csv")
    if err != nil {
        panic(err)
    }
    defer file.Close()

    // Create a csv.Reader object
    reader := csv.NewReader(nil)
    reader.Comma = ','

    // Create a bufio.Reader object
    bufReader := bufio.NewReader(file)

    // Create a utf16.Reader object
    utf16Reader := utf16.NewReader(bufReader)

    // Pass the utf16.Reader object to the csv.Reader object's Read method
    for {
        row, err := reader.Read()
        if err != nil {
            break
        }
        // Process the row here
    }
}

In python, I used codecs library but I am new in golang. Could anyone help me figure this out please. Thank you.

Golang error/solution

  • can you upload a csv demo? – Para Mar 06 '23 at 03:23
  • utf16 package does not have a reader. You can create one that uses an existing reader to read an even number of bytes, pass it through `encoding/binary/LittleEndian`, and then decode using `utf16` package functions. – Burak Serdar Mar 06 '23 at 04:20
  • Hello @Para, It is a csv file with normal data but utf-16 encoded. I need to create post body with every column from each row of the csv. But the problem is I could not read the csv utf16 encoded yet. Here is the sample of the csv https://drive.google.com/file/d/1pTMLfMQZFDOa4xPws9Yo78IjRlup9Bpg/view?usp=sharing – Nayem Shnartho Mar 06 '23 at 10:24
  • Hi @Burak, yes makes sense. Since I am a python developer and newbie in golang, I would request you, if possible, write a code sample. I would be more easier for me and others too to understand. – Nayem Shnartho Mar 06 '23 at 10:26
  • Hello @Steffen, no the solutions from 2013 and 2019 in that link could not actually help much with the problem I have with reading csv file utf16 encoded. – Nayem Shnartho Mar 06 '23 at 10:28
  • @NayemShnartho, the linked answers show exactly how to transform a utf-16 `io.Reader` which you can use to read the csv. If that doesn't work for you, then you need to show what you tried, and what didn't work. – JimB Mar 06 '23 at 13:56

0 Answers0