Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache Spark reading UTF-16 CSV file

I am trying to read a CSV file encoded in UTF-16.

val test = spark.read
.format("com.databricks.spark.csv")
.option("header", "true")
.option("inferSchema", "true")
.option("delimiter",";")
.option("dateFormat", "yyyy-MM-dd HH:mm:ss.SSS")
.option("encoding", "UTF-16")
.option("charset", "ISO-8859-1")
.load("...")

As a result I get extra lines:

enter image description here

Is it possible that Spark can only work with UTF-8 encoding? Or there is some other way to read UTF-16 CSV into a dataframe?

like image 862
datahack Avatar asked Jul 05 '26 03:07

datahack


1 Answers

I was also facing a similar issue while trying to read a csv file having UTF-16 format.

I am using mac and I didn't knew what is the encoding of the csv file that I was reading. Initially I was reading the file without providing any option for encoding and it was giving me two '?' in front of the column name.

So tried to find out the encoding of the csv file that I was reading by using below command in my mac.

file -I yourFile

The output of this command showed me that the encoding of the file is charset=utf-16le

Now I am reading the file by providing this options and it works fine.

val df = spark.read.
            format("csv").
            option("quote", "\"").
            option("escape", "\\").
            option("multiLine", "true").
            option("inferSchema", "true").
            option("header","true").
            option("encoding", "UTF-16").
            load(sourceS3path)

There is no needing of providing the option of charset if it is just reading the UTF-16 file.

like image 195
Nikunj Kakadiya Avatar answered Jul 06 '26 19:07

Nikunj Kakadiya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!