Get values from a text file in CSV format

Asked by frank101

I have managed to get sikuli to do what I want one time, but when I try to input a value from a text file and then loop the process for each value contained in the file, I can't even get past the first line of code, lol!

I tried searching for a solution but the closest I could find was this, but I get a SyntaxError: ("mismatched input 'in' expecting EOF", on line 1.

Here is the code I have so far, (I haven't even tried the loop yet, was trying to get the text input working first...)
My text file is basically like this:
"word1"
"word2"
"word3"
Do I need the " at the start and end of each line in the txt file, or just in Sikuli?

in Sikuli:
inp = file("path to your file")
content = inp.readlines()
for line in content:
   list = eval(line)
   (word) = eval(line)
<<<MY PROCESS>>>

Would I put the loop above the, in Sikuli: ? Or after it?

Any help would be very much appreciated!

Thanks, :)

Question information

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

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).

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

UPDATE: sorry for wrong indentation :-(

# 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

Revision history for this message
frank101 (frank101) said :
#3

Thank you very much for the reply RaiMan.

Works great! :D

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

Always welcome. pls. set to solved.

Revision history for this message
frank101 (frank101) said :
#5

Thanks RaiMan, that solved my question.