- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
How to simulate scanf() method using Python?
According to Python documentation
Python does not currently have an equivalent to scanf(). Regular expressions are generally more powerful, though also more verbose, than scanf()format strings. The table below offers some more-or-less equivalent mappings between scanf() format tokens and regular expressions.
scanf() TokenRegular Expression
|%e, %E, %f, %g||[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?|
To extract the filename and numbers from a string like
/usr/sbin/sendmail - 0 errors, 4 warnings
you would use a scanf() format like
%s - %d errors, %d warnings
The equivalent regular expression would be
(\S+) - (\d+) errors, (\d+) warnings
- Related Articles
- How to simulate Print screen button using selenium webdriver in Java?
- Problem with scanf() when there is fgets()/gets()/scanf() after it in C
- I want to call JButton doClick() method to simulate a click action in Java
- scanf() and fscanf() in C
- How to simulate discrete uniform random variable in R?
- How to simulate delayed and dropped packets on Linux?
- What is the common error occurred while using scanf() statement in C language?
- How to simulate HTML5 Drag and Drop in Selenium Webdriver?
- Difference between scanf() and gets() in C
- How to calculate catalan numbers with the method of Binominal Coefficients using Python?
- How to simulate touch event with android at a given position?
- How to simulate the LIMIT MySQL clause with an Access database?