Wednesday, September 14, 2022

How to enter tokenized data in Python

Ниже приведен пример кода на Пайтон 

(.env) [boris@sever35fedora TOKENS]$ cat inputTokens.py

import sys

tokenSequence= sys.stdin.readline().split(",")

try:

   x=float(tokenSequence[0])

   y=int(tokenSequence[1])

   z=complex(tokenSequence[2]) 

   print(type(x),type(y),type(z))

   print(x,y,z)

except ValueError as ex:

   print(ex)

 

(.env) [boris@sever35fedora TOKENS]$ python inputTokens.py

786.453,56783,12.7+5.6j

<class 'float'> <class 'int'> <class 'complex'>

786.453 56783 (12.7+5.6j)















boris@boris-All-Series:~/TOKENS$ cat inpuTokens.py

import sys

while True:

    tokenSequence= sys.stdin.readline().split(",")

    try:

       x=float(tokenSequence[0])

       if x == -1:

            break

       y=int(tokenSequence[1])

       z=complex(tokenSequence[2]) 

       print(type(x),type(y),type(z))

       print(x,y,z)

    except ValueError as ex:

       print(ex)

boris@boris-All-Series:~/TOKENS$ python3 inpuTokens.py

908.67,765,5+9j

<class 'float'> <class 'int'> <class 'complex'>

908.67 765 (5+9j)

345.78,342,8+2j

<class 'float'> <class 'int'> <class 'complex'>

345.78 342 (8+2j)

987.564,7865,5+7j

<class 'float'> <class 'int'> <class 'complex'>

987.564 7865 (5+7j)

-1

















No comments:

Post a Comment