what is the equivalent of scanf in python?
Tom Funk
_spam_sux_tdfunk at _spam_sux_nettally.com
Tue Mar 14 11:57:51 EST 2000
More information about the Python-list mailing list
Tue Mar 14 11:57:51 EST 2000
- Previous message (by thread): what is the equivalent of scanf in python?
- Next message (by thread): what is the equivalent of scanf in python?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
In an article posted Tue, 14 Mar 2000 15:43:12 +0000 (GMT), Oleg Broytmann (phd at phd.russ.ru) said: > On Tue, 14 Mar 2000, Shaun Hogan wrote: > > what is the equivalent of scanf in python? > > There is no direct scanf in Python - you should implement your own > parsing of input. Example: > > line = sys.stdin.readline() > x, y = string.split(line[:-1]) > x, y = int(x), int(y) You might also take a look at the split method in the re module. This will allow you to use regular expressions to chop up your data. >>> import re >>> import string >>> text = "name: John Doe, age: 29, Address: 1234 Main St." >>> pat = re.compile("[:,]") >>> fields=pat.split(text) >>> fields ['name', ' John Doe', ' age', ' 29', ' Address', ' 1234 Main St.'] >>> d={} # make a new empty dictionary >>> # for each item in fields, collect the field name and data >>> for I in range(0, len(fields)-1, 2): ... d[string.strip(fields[I])] = string.strip(fields[I+1]) ... >>> d # show what we did {'Address': '1234 Main St.', 'age': '29', 'name': 'John Doe'} >>> Enjoy! -- -=< tom >=- Thomas D. Funk (tdfunk at asd-web.com) | "Software is the lever Software Engineering Consultant | Archimedes was searching for" Advanced Systems Design, Tallahassee FL. |
- Previous message (by thread): what is the equivalent of scanf in python?
- Next message (by thread): what is the equivalent of scanf in python?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list