Scan until random delimiter.

L

Laurent Verweijen

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())


It works a bit but still requires the user to press enter.
Is there some way around it, which is platform independant?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top