0

In my Rails app, I managed to stream large CSV files directly from Postgres based on solutions mentioned in this SO post. My working code looks somewhat like so:

query = <A Long SQL Query String>
response.headers["Cache-Control"] = "no-cache"
response.headers["Content-Type"] = "text/csv; charset=utf-8"
response.headers["Content-Disposition"] =
     %(attachment; filename="#{csv_filename}")
response.headers["Last-Modified"] = Time.now.ctime.to_s
conn = ActiveRecord::Base.connection.raw_connection

conn.copy_data("COPY (#{query}) TO STDOUT WITH (FORMAT CSV, HEADER TRUE, FORCE_QUOTE *, ESCAPE E'\\\\');") do
    while row = conn.get_copy_data
      response.stream.write row
    end
  end
   response.stream.close
end

Some of the columns (VARCHAR) being queried have values as either English or Chinese strings. The CSV file resulting from the above code doesn’t show the Chinese characters as is. Instead, I get something like this:

大大 文文

Am I supposed to change the way I’m using the copy_data function, or is there something I could do to the CSV file to solve this? I’ve tried saving the file as UTF-8 .txt file, as well as trying the convert_to function mentioned in the copy_data documentation, but to no avail.

Lypyrhythm
  • 324
  • 2
  • 13
Aayush Kothari
  • 526
  • 3
  • 20

2 Answers2

0

This depends of the original encoding included in the CSV file.

Do this on Linux :

file -i you_file

Are you sure it's not UTF-16 or GB 18030 ? And also in what kind of encoding is setup your database ?

do a \l in psql to see this.

0

So it boiled down to my MS Excel not being able to render the Chinese chars correctly. On MacOS, opening the same .csv file using the Numbers app (or even Atom, for that matter) resolved this issue for me.

Aayush Kothari
  • 526
  • 3
  • 20