0

I've been struggling for the past day to solve a very simple (but frustratingly difficult) problem. I currently have a piece of code which uses csv reader to loop through csv files row by row. The loop is not an issue as I've used the same one before with success; it seems to be an issue with my if statements.

Here is a sample:

if 'INCORRECT_KEY_PRESSED' and 'rad_mod_KA_0043-lo-mod' in row:
 userAnswerColumn.append(row[20])
 userRtColumn.append(row[21])

This statement is supposed to append two lists with values from specific locations in a row, if that row contains the phrases INCORRECT_KEY_PRESSED and rad_mod_KA_0043-lo-mod. I have several such statements, but a row never has both of these phrases more than once. Phrases like rad_mod_KA_0043-lo-mod are always unique, while INCORRECT_KEY_PRESSED occurs many times.

It seems that the if statement is only using one of my conditions, as it is appending more than one piece of data to each list.

I've had this problem in the past and solved it by creating sets and doing something like this:

if (all(x in row for x in ssSet) and row[14] == '7'):
 ssColumn7.append(row[20])
 ssRtColumn7.append(row[21])

However, there are so many unique terms that I would have to make a plethora of sets. What's going wrong?

Martijn Pieters
1.1m326 gold badges4.2k silver badges3.4k bronze badges
asked Nov 8, 2012 at 15:38
1
  • Not sure why this is getting downvoted, it shows research effort and it's a common mistake. Commented Nov 8, 2012 at 15:51

3 Answers 3

3

The statement 'INCORRECT_KEY_PRESSED' and 'rad_mod_KA_0043-lo-mod' in row is interpreted as:

('INCORRECT_KEY_PRESSED') and ('rad_mod_KA_0043-lo-mod' in row)

The first part is always True (non-empty strings are 'truthy', or considered boolean True in a boolean expression).

You probably wanted:

if 'INCORRECT_KEY_PRESSED' in row and 'rad_mod_KA_0043-lo-mod' in row:

instead, or you could use set intersections:

if {'INCORRECT_KEY_PRESSED', 'rad_mod_KA_0043-lo-mod'}.intersection(row):
answered Nov 8, 2012 at 15:39
Sign up to request clarification or add additional context in comments.

2 Comments

The second part is still evaluated because both conditions must be true for and to be true.
Thank you for the help Mr. Pieters.
0

Your grouping is wrong, python works like:

if ('INCORRECT_KEY_PRESSED') and ('rad_mod_KA_0043-lo-mod' in row):

so you need:

if ('INCORRECT_KEY_PRESSED' in row) and ('rad_mod_KA_0043-lo-mod' in row):
answered Nov 8, 2012 at 15:40

Comments

0
if 'INCORRECT_KEY_PRESSED' and 'rad_mod_KA_0043-lo-mod' in row:
 userAnswerColumn.append(row[20])
 userRtColumn.append(row[21])

should be changed to :

if ('INCORRECT_KEY_PRESSED' in row) and ('rad_mod_KA_0043-lo-mod' in row):
 userAnswerColumn.append(row[20])
 userRtColumn.append(row[21])

because otherwise python understands

if ('INCORRECT_KEY_PRESSED') and 'rad_mod_KA_0043-lo-mod' in row:

which is probably always true.

You may want to think about that each time you have multiple conditions to test.

answered Nov 8, 2012 at 15:41

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.