# How to read data in text file and put into the web page

**URL:** <https://www.ruby-forum.com/t/how-to-read-data-in-text-file-and-put-into-the-web-page/188420>\
**Category:** Ruby\
**Created:** [May 11, 2010, 6:38pm UTC](https://www.ruby-forum.com/t/how-to-read-data-in-text-file-and-put-into-the-web-page/188420 "2010-05-11T18:38:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sweetzubair](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@sweetzubair](https://www.ruby-forum.com/u/sweetzubair)\
**Post date:** [May 11, 2010, 6:38pm UTC](https://www.ruby-forum.com/t/how-to-read-data-in-text-file-and-put-into-the-web-page/188420/1 "2010-05-11T18:38:05Z")

</div>

Hi i want to read data in the text file like.

username=abcd  
password=1234

i write code through the counter and i want write code without counter.  
Plz help me…

require ‘watir’  
include Watir

counter = 1

ie=Watir::IE.start(“[http://www.google.co.in](http://www.google.co.in)”)  
file = File.new(“c:/test/test.txt”, “r”)

while (line = file.gets ‘=’)  
line = file.readline  
while(counter == 1)  
ie.text\_field(:name,“userId”).set(line)  
break  
end  
while(counter == 2)  
ie.text\_field(:name,“password”).set(line)  
break  
end  
counter = counter + 1  
end  
file.close  
ie.button(:name,“Submit”).click()

---

<div class="post-metadata">

**Author:** ![sweetzubair](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@sweetzubair](https://www.ruby-forum.com/u/sweetzubair)\
**Post date:** [May 11, 2010, 7:52pm UTC](https://www.ruby-forum.com/t/how-to-read-data-in-text-file-and-put-into-the-web-page/188420/2 "2010-05-11T19:52:28Z")

</div>

\*\*\*\*\*\*\* It’s Realy works \*\*\*\*\*\*\*\*\*\*\*\*\*\*

Thanks a lotttttttttttttt…!!!1

---

<div class="post-metadata">

**Author:** ![sweetzubair](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@sweetzubair](https://www.ruby-forum.com/u/sweetzubair)\
**Post date:** [May 11, 2010, 6:49pm UTC](https://www.ruby-forum.com/t/how-to-read-data-in-text-file-and-put-into-the-web-page/188420/3 "2010-05-11T18:49:16Z")

</div>

On Tue, May 11, 2010 at 6:38 PM, Zubair A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Hi i want to read data in the text file like.
> 
> username=abcd  
> password=1234
> 
> i write code through the counter and i want write code without counter.  
> Plz help me…

You have a file with just those two variables? Then you can do this:

contents = File.read(“c:/test/test.txt”)  
data = contents.map do |line|  
line.chomp.split(“=”)[1]  
end

Then you have

data[0] #=\> abcd  
data[1] #=\> 1234

and you can use those directly:

ie.text\_field(:name,“userId”).set(data[0])  
ie.text\_field(:name,“password”).set(data[1])

Hope this helps,

Jesus.

---

<div class="post-metadata">

**Author:** ![sweetzubair](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7feea3/32.png) [@sweetzubair](https://www.ruby-forum.com/u/sweetzubair)\
**Post date:** [May 11, 2010, 11:50pm UTC](https://www.ruby-forum.com/t/how-to-read-data-in-text-file-and-put-into-the-web-page/188420/4 "2010-05-11T23:50:38Z")

</div>

Zubair A. wrote:

> Hi i want to read data in the text file like.
> 
> username=abcd  
> password=1234
> 
> i write code through the counter and i want write code without counter.

Building a Hash of your input data is one way to make it convenient to  
access:

opts = {}  
File.open(“test.txt”) do |f|  
f.each\_line do |line|  
opts[$1] = $2 if line =~ /^(._)=(._)$/  
end  
end  
puts opts[“username”]  
puts opts[“password”]

A lot of Rubyists use a YAML input file instead, which would have the  
form

username: abcd  
password: “1234”

(note the quotes needed to make it parse as a string rather than a  
number). Then it just becomes:

require ‘yaml’  
opts = YAML.load\_file(“test.yaml”)
