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 7826155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:16:50+00:00 2026-06-02T09:16:50+00:00

I am generating a CSV file from Ruby, and the system that is receiving

  • 0

I am generating a CSV file from Ruby, and the system that is receiving the data can’t accept a blank line at the bottom.

However, after I’ve appended all my lines (that end with CRLF) the last row still has the CFLF, so the receiving system think that there is a blank lasy line.

I solved it by doing this (hackish) code to open and truncate the file:

  size = File.size(filename)
  File.truncate(filename, size-3)

Am I missing some feature in ruby CSV to not add CRLF if it is the last line?

My code for writing the CSV is:

  csv = CSV.open(filename, "w", {:row_sep => "\r\n"})
  .. code to create an array of cols ..
  csv << cols

I tried converting this to use a string, rather then a file. But I had no success doing it. I kept getting an error using the << operator.

UPDATE: It turns out I had the wrong question…

It’s been a while but I finally figured out what was going on. The host system I was uploading too can handle the newline at the end of the file – it was that Ruby CSV was putting in the wrong newline characters: \r\r\n instread of \r\n! It is doing this if I write as a text file. If I write as a binary file the problem is solved.

In case anyone else sees the same issue, this was my Ruby:

csv = CSV.open(filename, "w", {:row_sep => "\r\n"}) # actually writes \r\r\n 

Binary mode fixes it:

csv = CSV.open(filename, "wb", {:row_sep => "\r\n"}) # correctly writes \r\n
  • 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-02T09:16:56+00:00Added an answer on June 2, 2026 at 9:16 am

    If you are getting the CSV as a string before writing it, you can use String#chomp to remove a trailing newline.

    If you are writing the file by using:

    File.open('my.csv','w'){ |f| f.puts my_csv }
    

    …then you are adding a newline yourself, and can avoid it with:

    File.open('my.csv','w'){ |f| f.write my_csv }
    

    If you supply your actual code for generating and writing the CSV, we can help you further and more precisely.

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

Sidebar

Related Questions

I have a large nested array that I'm generating from parsing a CSV file
I am generating a CSV file from Ruby. The problem is a column string
I have an XSL file that I am generating from CSV from and Object
I am generating a CSV file containing data from a MYSQL database. There are
I'm generating a CSV file from the following code public ActionResult Index() { var
Have a program that's dynamically generating an Excel file and a csv. The excel
I am importing some data from an file (xls, csv, xml) wich will result
I have written application that is analyzing data and writing results in CSV file.
I am using C++ and I am generating a csv file to report some
I'm generating a CSV from an MVC 3 website and using a FileContentResult to

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.