Scan until random delimiter.

Albert Hopkins marduk at letterboxes.org
Sun Jun 27 17:10:39 EDT 2010


On Sun, 2010-06-27 at 22:41 +0200, Laurent Verweijen wrote:
> In contrast to java or c python seems not be able to use a random
> delimiter.
> 
> In java, you can do:
> 
> 
> Code:
> 
> import java.util.Scanner
> 
> Scanner sc = new Scanner(System.in).useSeperator(" ")
> int a = sc.nextInt()
> 
> 
> But in python there seems to be no other option then waiting until you
> see a newline.
> I wrote a script which should allow more freedom.
> 
> 
> Code:
> 
> #!/usr/bin/python
> 
> def readtoken(source=None, delim=" \n\t\r"):
>         if source is None:
>                 from sys import stdin
>                 source = stdin
> 
>         r = []
>         c = delim + " "
> 
>         while c not in delim:
>                 c = source.read(1)
>                 r.append(c)
> 
>         return "".join(r)
> 
> if __name__ == "__main__":
>         for _ in range(5):
>                 print(readtoken())
> 

I found this recipe (though not tried it):

http://code.activestate.com/recipes/134892/







More information about the Python-list mailing list