← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #146518]: Get value from a text file, then loop process until end of text file

 

Question #146518 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/146518

    Status: Open => Answered

RaiMan proposed the following answer:
this is sufficient and works:

sample file:

"word1", 1
"word2", 2
"word3", 3

--- field seperator should be comma and values have be valid Python

# form 1: each field in a variable
inp = file("path to your file")
content = inp.readlines()
for line in content:
	(wrd, nmbr) = eval(''.join(('[', line.strip(), ']')))
        print "word:", wrd, "number:", nmbr

# form 2: all fields in a list (array)
inp = file("path to your file")
content = inp.readlines()
for line in content:
    flds = eval(''.join(('[', line.strip(), ']')))
    print flds

access to each field: flds[n]  --- 1st index is 0 !

--- comment on eval(''.join(('[', line.strip(), ']')))
builds and evals an expression like [field1, field2, ...] after stripping the trailing linefeed. This is the definition of a list (mutable array).

You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.