Sunday, April 3, 2011

python string input problem with whitespace!!

my input is something like this

23 + 45 = astart

for the exact input when i take it as raw_input() and then try to split it , it gives me an error like this

SyntaxError: invalid syntax

the code is this

k=raw_input()
a,b=(str(i) for i in k.split('  +   '))
b,c=(str(i) for i in b.split('  =   '))

its always number + number = astar

its just that when i give number+number=astar i am not getting syntax error ..!! but when i give whitespace i get sytax error

From stackoverflow
  • Edit: as pointed out by Triptych, the generator object isn't the problem. The partition solution is still good and holds even for invalid inputs

    calling (... for ...) only returns a generator object, not a tuple

    try one of the following:

    a,b=[str(i) for i in k.split('  +   ')]
    a,b=list(str(i) for i in k.split('  +   '))
    

    they return a list which can be unpacked (assuming one split)

    or use str.partition assuming 2.5 or greater:

    a, serperator, b = k.partition('+')
    

    which will always return a 3 tuple even if the string isn't found

    Edit: and if you don't want the spaces in your input use the strip function

    a = a.strip()
    b = b.strip()
    

    Edit: fixed str.partition method, had wrong function name for some reason

    mekasperasky : still syntax error
    Triptych : -1. Generators can be unpacked just fine. Try a,b = (i for i in (1,2)) for confirmatino.
  • Testing with Python 2.5.2, your code ran OK as long as I only had the same spacing on either side of the + and = in the code and input.

    You appear to have two spaces on either side of them in the code, but only one on either side in the input. Also - you do not have to use the str(i) in a generator. You can do it like a,b=k.split(' + ')

    My cut and pastes:
    
    My test script:
    
    print 'Enter input #1:'
    k=raw_input()
    
    a,b=(str(i) for i in k.split(' + '))
    b,c=(str(i) for i in b.split(' = '))
    
    print 'Here are the resulting values:'
    print a
    print b
    print c
    
    
    print 'Enter input #2:'
    k=raw_input()
    
    a,b=k.split(' + ')
    b,c=b.split(' = ')
    
    print 'Here are the resulting values:'
    print a
    print b
    print c
    
    
    From the interpreter:
    
    >>> 
    Enter input #1:
    23 + 45 = astart
    Here are the resulting values:
    23
    45
    astart
    Enter input #2:
    23 + 45 = astart
    Here are the resulting values:
    23
    45
    astart
    >>>
    
    lyrae : Works fine here for me too. And if i take out the spaces, i get ValueError, not syntax error. If it was me, i would just do "a, b = (i for i in k.split('+')", without spaces.
  • I think I'd just use a simple regular expression:

    # Set up a few regular expressions
    parser = re.compile("(\d+)\+(\d+)=(.+)")
    spaces = re.compile("\s+")
    
    # Grab input
    input = raw_input()
    
    # Remove all whitespace
    input = spaces.sub('',input)
    
    # Parse away
    num1, num2, result = m.match(input)
    
  • You could just use:

    a, b, c = raw_input().replace('+',' ').replace('=', ' ').split()
    

    Or [Edited to add] - here's another one that avoids creating the extra intermediate strings:

    a, b, c = raw_input().split()[::2]
    

    Hrm - just realized that second one requires spaces, though, so not as good.

  • Rather than trying to solve your problem, I thought I'd point out a basic step you could take to try to understand why you're getting a syntax error: print your intermediate products.

    k=raw_input()
    print k.split('  +   ')
    a,b=(str(i) for i in k.split('  +   '))
    print b.split('  =   ')
    b,c=(str(i) for i in b.split('  =   '))
    

    This will show you the actual list elements produced by the split, which might shed some light on the problem you're having.

    I'm not normally a fan of debugging by print statement, but one of the advantages that Python has is that it's so easy to fire up the interpreter and just mess around interactively, one statement at a time, to see what's going on.

0 comments:

Post a Comment