geting data from a file

Asked by TomASS

I've a form with three fields and one button and I've a file with several lines - each line contains from three secrion

1field | 2field | 3 field |
1field | 2field | 3 field |
1field | 2field | 3 field |

(I'm genertaing a file so - i can generate other).

How to write
1. loop
2. read each line, and explode it for three secton?

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

If its possible for you to generate the file, the easiest approach would be:

your file:
[ 1field, 2field, 3field ]
[ 1field, 2field, 3field ]
[ 1field, 2field, 3field ]

where numbers can be written down directly and text has to be written as "... some text ...".
its written down in python syntax for lists, so we can use eval().

in Sikuli:
inp = file("path to your file")
xcontent = inp.readlines() # all lines are read into a list [ line1, line2, line3, ...]
for line in content: # loops thru list and line gets every line
   list = eval(line) # now you have list = [ 1field, 2field, 3field ] as list of your fields
   # accessible by list[0] ... or another loop
   # or if the number of fields is known and fixed:
   (f1, f2, f3) = eval(line) # you have f1=field1 ...

mind indentation - its python

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

sorry, typing error:

content = inp.readlines() # all lines are read into a list [ line1, line2, line3, ...]

instead of
xcontent = inp.readlines() # all lines are read into a list [ line1, line2, line3, ...]

Revision history for this message
alcia burke (alcianicole) said :
#3

Hi

I tried using:
content = inp.readlines()
for line in content:
   list = eval(line)
   (f1, f2, f3) = eval(line)

However, "list = eval(line)" gives a Lexical Error.

What am I doing wrong?

I am importing a csv file

Thanks

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

I think, you have to say
list = eval("["+line+"]")
since list on the left needs an explicit list on the right.

the term
(f1, f2, f3) = eval(line)
is not really a list, since only 3 individual values are mapped to the right side.

Revision history for this message
alcia burke (alcianicole) said :
#5

Hi

Can you give me a working example of "(f1, f2, f3) = eval(line)". I am not still new to coding.

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

if line is a string containing for example "1, 'text', 56" then saying
(f1, f2, f3) = eval(line)

will lead to
f1 containing 1
f2 containing 'text'
f3 containing 56

and the other example
list = eval("["+line+"]")
will give
list as a refernce to a list object: [ 1, 'text', 56 ]

eval(something) works only if something is a string containing a valid expression.

Can you help with this problem?

Provide an answer of your own, or ask TomASS for more information if necessary.

To post a message you must log in.