Calling a data from TXT file

Asked by Anurag

Hello guys,

I need some urgent help from you guys, Actually i need to call a data from notepad line by line according to my usage say
if txt looks like
name Anurag age 12 sex male.
name Lokesh age 24 sex male.
name Ashu age 23 sex male.
and so on
i like to call name in script, it should reflect as Anurag and age as 12 and sex as male.
for the second time it should reflect lokesh, 24 and male so on.

Please help me i really need this.

thanks in advance.

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Anurag
Solved:
Last query:
Last reply:

This question was reopened

Revision history for this message
RaiMan (raimund-hocke) said :
#1

lines = open("notepad file.txt").readlines()
for line in lines:
    fields = line.split()
    name = fields[1]
    age = fields[3]
    sex = fields[5]

if you need age as number:
    age = int( fields[3])

Revision history for this message
Anurag (annubhav10) said :
#2

Thanks for the revert Raiman!!!!!!

Yes it solve my problem some extent

I need to call data from other txt also, I already tried the same thing that you suggest but nested for loop is not working , It works fyn for a one go but for the next time it comes on the second FOR loop instead of going again on first one.

Is there any mean like i can call the first loop.

Revision history for this message
Anurag (annubhav10) said :
#3

see my script, and please help me!!!!!!

file = open("c:\\temp1\\t.txt", "r")
ord = file.read()
file.close()
list = ord.splitlines()

for x in list:
    plan = "AM15.019001-"
    #find("SprintCapaci.png")
    h = find("ToolBoxSearc.png")
    r = h.find("Search.png")
    click(r)
    wait(4)
    click(Pattern("CircuitID.png").targetOffset(-64,8))
    wait(2)
    type(plan)
    type(x)
    type("\n")
    wait(10)
    type( u"\ue002")
    type(u"\ue002")
    wait(1)
    j=find("CircuitResul.png")
    hover(click(Pattern("Select.png").targetOffset(0,1)))
    wait(4)
    lines = open("c:\\temp1\\th.txt").readlines()
    for line in lines:
        fields = line.split()
        circuit = fields[1]
        ecckt = fields[3]
        comment = fields[5]
        #print circuit
      # print ecckt
      # print comment

        click(Pattern("ECCKT.png").targetOffset(-73,10))
        wait(1)
        type(ecckt)
        find("AddNote_.png")
        doubleClick(Pattern("AddNote_.png").targetOffset(-411,19))
        wait(1)
        type(comment)
        wait(2)
        type("\t")
        type( u"\ue004")
        wait(2)
        t=click(Pattern("CircuitDetai.png").targetOffset(-109,17))
        click(t)
        wait(3)
        type("a", KEY_CTRL)
        type(u"\ue006")
        wait(1)
        type(circuit)
        click(Pattern("POPPopisNewl.png").targetOffset(-174,-17))
        wait(3)
        type( u"\ue005")
        wait(1)
        click(Pattern("SaveCircuitD-1.png").targetOffset(-128,1))
        wait(2)

Revision history for this message
Anurag (annubhav10) said :
#4

Thanks RaiMan, that solved my question.

Revision history for this message
Anurag (annubhav10) said :
#5

please ignore the last comment and helped me in my query...................

Revision history for this message
Anurag (annubhav10) said :
#6

Please help me in my query i reallly needs this one...

waiting for your reply guys............

HELP ME HELP ME HELP ME HELP ME!!!!!!!!!!!!

Revision history for this message
RaiMan (raimund-hocke) said :
#7

did you get further?

not really sure, what your problem is.

Try to tell with your own words, what you want to achieve (use some pseudo code without too much details), instead of posting detailed code, that might not reflect your intention.

Revision history for this message
Anurag (annubhav10) said :
#8

ay my file look like this.
name Anurag age 12 sex male.
name Lokesh age 24 sex male.
name Ashu age 23 sex male.

and my code looks like
for i in range(1,4):
type("PLAN"+str(i))
#afterwards i used ur codes to fetch data from file
lines = open("notepad file.txt").readlines()
for line in lines:
    fields = line.split()
    name = fields[1]
    age = fields[3]
    sex = fields[5]

type(name)
type(age)
type(sex)
break

It works fyn when i=1, it reflects name as Anurag, sex as male and age as 12.
however problem arises when i=2 and it reflect same result as Anurag, male and 12 but i required next line data(from txt file) which is lokesh, male and 24.
and so on like for i=3 it should shows ashu, male 23.

Revision history for this message
RaiMan (raimund-hocke) said :
#9

i = 0
lines = open("notepad file.txt").readlines()
for line in lines:
    i += 1
    type("PLAN"+str(i))
    fields = line.split()
    name = fields[1]
    age = fields[3]
    sex = fields[5]
    type(name)
    type(age)
    type(sex)

Since you want to do something for each line in your file, the outer loop must step through ones of the file.

Revision history for this message
Anurag (annubhav10) said :
#10

thanks Rai man,

Yes my problem is solved..........

thanks for your efforts!!!!!!!!!!!!

Revision history for this message
Anurag (annubhav10) said :
#11

Raiman !!!!

A general query from my side....

the above script provide output by counting fields in notepad correct.. and
say my txt looks like
name Anurag Gupta age 12 sex male.
name Lokesh Sharma age 24 sex male.
name Ashu Gupta age 23 sex male.

Above script provide output as
Anurag
12
male

and soon

now i required name as Anurag Gupta
12
male

and so on

Please help me in this queery!!!!!

Revision history for this message
RaiMan (raimund-hocke) said :
#12

 = 0
lines = open("notepad file.txt").readlines()
for line in lines:
    i += 1
    type("PLAN"+str(i))
    fields = line.split()
    k = 3
    n = 2
    name = ""
    while fields[n] != "age":
        k += 1
        n += 1
        name += " " + fields[n]
    age = fields[k]
    sex = fields[k + 2]

Revision history for this message
Anurag (annubhav10) said :
#13

Raiman,

It shows me a error suggested below
'name += " " + fields[n]
IndexError: index out of range: 7'

while executing the above code to print (name),print(age)....

Revision history for this message
RaiMan (raimund-hocke) said :
#14

ok, my fault:

i = 0
lines = open("notepad file.txt").readlines()
for line in lines:
    i += 1
    type("PLAN"+str(i))
    fields = line.split()
    k = 2
    n = 1
    name = ""
    while fields[n] != "age":
        name += " " + fields[n]
        k += 1
        n += 1
    age = fields[k]
    sex = fields[k + 2]

in my test with your above data I got this:

PLAN1
Anurag Gupta
12
male

PLAN2
Lokesh Sharma
24
male

PLAN3
Ashu Gupta
23
male

Revision history for this message
Anurag (annubhav10) said :
#15

Thanks Raiman!!!!!!

yes its solves my problem !!!!

Thank you so muchhh.........you rocked