CSV Reading Name Error....

Asked by David Lafferty

I'm currently raging at this, it worked a little earlier but then I added a new line and now it's stopped altogether.
Can anyone point out the obv flaw?

Sikuli code:
---------------------------------------------------------------------
import csv
xReader = file("C:\\Users\\davidl01\\Desktop\\Sikuli-ModelOffice0_Release\\newbie.csv")
xReader.readline()
for line in xReader:
   (ref, val) = eval(''.join(('[', line.strip(), ']')))
   if (Ref == "Smoker"):
       Settings.Smoker = Val
   if (Ref == "Gender"):
       Settings.Gender = Val
   if (Ref == "Marital"):
       Settings.Marital = Val
   if (Ref == "Forename"):
       Settings.Forename = Val
   if (Ref == "DOB"):
       Settings.DOB = Val
print Settings.Smoker
print Settings.Gender
print Settings.Marital
print Settings.Forename
print Settings.DOB
---------------------------------------------------------
newbie.csv file
---------------------------------------------------------
Smoker,n
Gender,m
Marital,s
Forename,Test
DOB,t-50y

---------------------------------------------------------
Error Message
---------------------------------------------------------
[error] An error occurs at line 5
[error] Error message: Traceback (most recent call last):
 File "C:\Users\davidl01\AppData\Local\Temp\sikuli-tmp1905465301904626220.py", line 5, in
 (ref, val) = eval(''.join(('[', line.strip(), ']')))
 File "", line 1, in
NameError: name 'Gender' is not defined

---------------------------------------------------------
Before people ask, I've made sure the files are all in the correct place etc and saved in the .CSV format.
If I'm being a tool don't hold back, otherwise I won't learn. :)
Thanks,
Dave

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
Best RaiMan (raimund-hocke) said :
#1

see comments in code:

import csv # not used
# use raw string r"....", no dooble backslash needed
xReader = file(r"C:\Users\davidl01\Desktop\Sikuli-ModelOffice0_Release\newbie.csv")
# xReader.readline() # this reads the first line, and it is not used! delete!
for line in xReader: # this loops through the rest of lines / all the lines
    # should be Ref and Val
    (Ref, Val) = line.strip().split(",")
    # see comment for the "eval" statement
    if (Ref == "Smoker"):
       Settings.Smoker = Val
    if (Ref == "Gender"):
       Settings.Gender = Val
    if (Ref == "Marital"):
       Settings.Marital = Val
    if (Ref == "Forename"):
       Settings.Forename = Val
    if (Ref == "DOB"):
       Settings.DOB = Val
print Settings.Smoker
print Settings.Gender
print Settings.Marital
print Settings.Forename

--- comment on your eval statement

(ref, val) = eval(''.join(('[', line.strip(), ']')))

since you missed the first line, the second one is the first, that is used:
Gender,m

and it produces:
[Gender,m]

so Gender is seen as a variable name (which is not defined), and not as a string.

If it looked this way, it would work:
["Gender","m"]

but this is not manageable with your code. The apostrophes would have to be in the csv file.

But the solution is much easier, using your csv file, as you can see above ;-)

print Settings.DOB

Revision history for this message
David Lafferty (david-lafferty) said :
#2

Raiman, you are simply a legend.

I hacked my way around this for the time being by sticking single quotes around every item within the csv file and the first entry being blank.

But I've altered the code as per your advice and I'm cooking with gas now.

I had thought that the xReader.readLine() part was required because I thought it retrieved each "line" for use within the For Loop.

Thanks again!

Dave

Revision history for this message
David Lafferty (david-lafferty) said :
#3

Thanks RaiMan, that solved my question.