Read text file

P

Pragash Mr.

Hi,
I have one text file for example


9115 2008-06-20 07:26:35
91415 2008-06-20 07:26:32
9315 2008-06-20 07:26:33
2115 2008-06-20 07:26:05
15 2008-06-20 07:26:34
115 2008-06-20 07:26:35


i need to read only first column i.e numbers like 9115 etc if you have
any solution reply me
 
M

Mateusz Tybura

Pragash said:
Hi,
I have one text file for example


9115 2008-06-20 07:26:35
91415 2008-06-20 07:26:32
9315 2008-06-20 07:26:33
2115 2008-06-20 07:26:05
15 2008-06-20 07:26:34
115 2008-06-20 07:26:35


i need to read only first column i.e numbers like 9115 etc if you have
any solution reply me

Try this:

file = File.open("name.txt")
columns = []
file.each_line do |line|
columns << line.split(" ")[0]
end
p columns
 
S

Sebastian Hungerecker

Pragash said:
15 =C2=A02008-06-20 07:26:34
115 =C2=A02008-06-20 07:26:35

i need to read only first column i.e numbers like 9115 etc if you have
any solution reply me

numbers =3D File.open(filename) do |f|
f.map do |line|
line.to_i
end
end=20

HTH,
Sebastian
=2D-=20
Jabber: (e-mail address removed)
ICQ: 205544826
 
A

Andrés Suárez

Hi, there is another solution:

File.open('pp.txt') do |infile|
while line = infile.gets #the loop ends if infile.gets == EOF
line_split = line.split(" ") #split line through spaces; return
vector
puts line_split[0] #the first element of the line is the
number
end
end

for more information of this topic:

http://pleac.sourceforge.net/pleac_ruby/fileaccess.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,812
Messages
2,569,694
Members
45,478
Latest member
dontilydondon

Latest Threads

Top