Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8354897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:41:55+00:00 2026-06-09T09:41:55+00:00

I’m parsing a CSV file that I’ve pulled from an FTP site. I want

  • 0

I’m parsing a CSV file that I’ve pulled from an FTP site. I want to parse the CSV and extract some specific fields to store in the database. I encounter some encoding I don’t understand and I believe CSV.parse also isn’t expecting the encoding:

filename = "#{RAILS_ROOT}/spec/files/20120801.01.001.CSV"
filestream = File.new(filename, "r")
while (line = filestream.gets)
  puts "line: #{line}"
  CSV.parse(line) do |row|
    case row[0]
    when "RH"
     # do something
    when "SH"
     #do something else
    end
  end
end

The first line in the CSV file looks something like this:

"\376\377\000\"\000R\000H\000\"\000,\0002\0000\0004\0005\000/\0000\0008\000/\0000\0002\000 \0000\0005\000:\0005\0007\000:\0002\0001\000 \000-\0000\0007\0000\0000\000,\0002\0000\0001\0002\000/\0000\0008\000/\0000\0001\000 \0000\0000\000:\0000\0000\000:\0000\0000\000 \000-\0000\0004\0000\0000\000,\0002\0000\0001\0002\000/\0000\0008\000/\0000\0001\000 \0002\0003\000:\0005\0009\000:\0001\0004\000 \000-\0000\0007\0000\0000\000,\000\"\000Y\0003\000B\0003\0003\000Z\000N\000K\000A\000U\000B\000H\000N\000\"\000,\0000\0000\0001\000,\000\n"

I have a different CSV file that I created myself and it prints out as human-readable text. What am I missing here? Do I need to apply a some encoding to the CSV string before passing to CSV.parse.

Here’s the stacktrace:

CSV::IllegalFormatError
/Users/project/app/models/parse_csv.rb:5:in `parse'

I am forced to use ruby v1.8.7 at the moment.

I know that I could use CSV.open, but I’m intentionally trying to feed CSV.parse an IO stream so that I can grab CSV files from an FTP site using SFTP to stream the files into memory without having to store the CSV file to disk:

 sftp.open_handle("/path/to/remote.file") do |handle|     
      data = sftp.read(handle)   
   end

Thanks in advance for any ideas!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T09:41:56+00:00Added an answer on June 9, 2026 at 9:41 am

    The line has double quotes in it which may need to be escaped. I found this on ruby-forum.com.

    It’s just a guess, but maybe you could try replacing every
    double-quote character that isn’t either preceded or followed by a
    comma with a single quote? Something like the untested code below:

    line.gsub(/[^,]”[^,]/,”‘”)

    It would probably require reading the whole file first, writing out a
    corrected version, and then calling the CSV methods on that, but it
    beats doing it by hand :).

    Also, as an aside, I think instead of

    while (line = filestream.gets)

    you could do

    filestream.gets.each_line do |line|

    which might be more rubyish (maybe?)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
i want to parse a xhtml file and display in UITableView. what is the
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an XML file, the creators of it stuck in a bunch social
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.