Project

General

Profile

Bug #4515

File.each_line read all lines of file as one line

Added by maso (Yen-Nan Lin) about 8 years ago. Updated about 8 years ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
Target version:
-
ruby -v:
-
Backport:
[ruby-core:35534]

Description

=begin
I access a tab-delimited text file from Excel, the text file is 5 lines in my editors (TextEdit, TextWrangler).

However, when I use File.each_line to read it, this method only iterate once and combine 5 lines to 1 line.

I expect my code to puts count = 5, but it print 1.

Below is my code, the test2.txt file is in attachment if you need.


filePath = "test2.txt"

data = Array.new

File.open(filePath, 'r') do |f|
count = 0
f.each_line do |line|
data << line.split.map { |entry| entry.to_i }
count += 1
end
puts count

end


I find a solution in http://railsforum.com/viewtopic.php?id=15582

But I think this bug should be solved.

Thank you very much!!

Yen-Nan Lin
=end


Files

test2.txt (49 Bytes) test2.txt maso (Yen-Nan Lin), 03/21/2011 10:05 PM

History

Updated by matz (Yukihiro Matsumoto) about 8 years ago

  • ruby -v changed from ruby 1.9.2p180 (2011-02-18 revision 30909) [i386-darwin9.8.0] to -

=begin
Hi,

The default line separator is a newline ("\n"), but if you want to
change it, try f.each_line("\r") instead.

                        matz.

In message "Re: [ruby-core:35534] [Ruby 1.9 - Bug #4515][Open] File.each_line read all lines of file as one line"
on Mon, 21 Mar 2011 22:05:34 +0900, Yen-Nan Lin redmine@ruby-lang.org writes:

|I access a tab-delimited text file from Excel, the text file is 5 lines in my editors (TextEdit, TextWrangler).
|
|However, when I use File.each_line to read it, this method only iterate once and combine 5 lines to 1 line.
|
|I expect my code to puts count = 5, but it print 1.
|
|Below is my code, the test2.txt file is in attachment if you need.
|
|----------------------------------------------------
|
|filePath = "test2.txt"
|
|data = Array.new
|
|File.open(filePath, 'r') do |f|
| count = 0
| f.each_line do |line|
| data << line.split.map { |entry| entry.to_i }
| count += 1
| end
| puts count

|end
|
|----------------------------------------------------------
|
|I find a solution in http://railsforum.com/viewtopic.php?id=15582
|
|But I think this bug should be solved.
|
|Thank you very much!!
|
|Yen-Nan Lin
|
|
|--
|http://redmine.ruby-lang.org
|
=end

Updated by naruse (Yui NARUSE) about 8 years ago

  • Status changed from Open to Rejected

=begin

=end

Updated by matz (Yukihiro Matsumoto) about 8 years ago

=begin
Hi,

The default line separator is a newline ("\n"), but if you want to
change it, try f.each_line("\r") instead.

                        matz.

In message "Re: [ruby-core:35534] [Ruby 1.9 - Bug #4515][Open] File.each_line read all lines of file as one line"
on Mon, 21 Mar 2011 22:05:34 +0900, Yen-Nan Lin redmine@ruby-lang.org writes:

|I access a tab-delimited text file from Excel, the text file is 5 lines in my editors (TextEdit, TextWrangler).
|
|However, when I use File.each_line to read it, this method only iterate once and combine 5 lines to 1 line.
|
|I expect my code to puts count = 5, but it print 1.
|
|Below is my code, the test2.txt file is in attachment if you need.
|
|----------------------------------------------------
|
|filePath = "test2.txt"
|
|data = Array.new
|
|File.open(filePath, 'r') do |f|
| count = 0
| f.each_line do |line|
| data << line.split.map { |entry| entry.to_i }
| count += 1
| end
| puts count

|end
|
|----------------------------------------------------------
|
|I find a solution in http://railsforum.com/viewtopic.php?id=15582
|
|But I think this bug should be solved.
|
|Thank you very much!!
|
|Yen-Nan Lin
|
|
|--
|http://redmine.ruby-lang.org
|
=end

Updated by matz (Yukihiro Matsumoto) about 8 years ago

=begin
Hi,

The default line separator is a newline ("\n"), but if you want to
change it, try f.each_line("\r") instead.

                        matz.

In message "Re: [ruby-core:35534] [Ruby 1.9 - Bug #4515][Open] File.each_line read all lines of file as one line"
on Mon, 21 Mar 2011 22:05:34 +0900, Yen-Nan Lin redmine@ruby-lang.org writes:

|I access a tab-delimited text file from Excel, the text file is 5 lines in my editors (TextEdit, TextWrangler).
|
|However, when I use File.each_line to read it, this method only iterate once and combine 5 lines to 1 line.
|
|I expect my code to puts count = 5, but it print 1.
|
|Below is my code, the test2.txt file is in attachment if you need.
|
|----------------------------------------------------
|
|filePath = "test2.txt"
|
|data = Array.new
|
|File.open(filePath, 'r') do |f|
| count = 0
| f.each_line do |line|
| data << line.split.map { |entry| entry.to_i }
| count += 1
| end
| puts count

|end
|
|----------------------------------------------------------
|
|I find a solution in http://railsforum.com/viewtopic.php?id=15582
|
|But I think this bug should be solved.
|
|Thank you very much!!
|
|Yen-Nan Lin
|
|
|--
|http://redmine.ruby-lang.org
|
=end

Also available in: Atom PDF