python - And and Or confusion -


i have file i'm parsing, on each line in same column 1 of values; 0, 1 or -1. want parse numeric values in line below. trying via if statement and/or operators. cannot head around and/or when test print statement, output either prints 1's or prints 0's.

 desired output:  0  1 -1  if number.startswith('1' , '0'):     return number.append(' ') print number 

'1' , '0' resolves '0':

>>> '1' , '0' '0' 

boolean operators not same thing english grammatical constructs!

the str.startswith() method takes tuple of values test for:

number.startswith(('1', '0')) 

and it'll return true if string starts '1' or starts '0'.

however, in case can use string formatting pad out number:

return format(int(number), '-2d') 

this pads number out field 2 characters wide, leaving space negative sign:\

>>> format(-1, '-2d') '-1' >>> format(0, '-2d') ' 0' >>> format(1, '-2d') ' 1' 

Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -