sscanf in Python
Python doesn't have an sscanf
equivalent built-in, and most of the time it actually makes a whole lot more sense to parse the input by working with the string directly, using regexps, or using a parsing tool.
Probably mostly useful for translating C, people have implemented sscanf
, such as in this module: http://hkn.eecs.berkeley.edu/~dyoo/python/scanf/
In this particular case if you just want to split the data based on multiple split characters, re.split
is really the right tool.
String splitting like sscanf in Python
split is all you need.
line.split(None, 2)
Docs for split with emphasis added:
string.split(s[, sep[, maxsplit]])
Return a list of the words of the string s. If the optional second argument sep is absent or None, the words are separated by arbitrary strings of whitespace characters (space, tab, newline, return, formfeed). If the second argument sep is present and not None, it specifies a string to be used as the word separator. The returned list will then have one more item than the number of non-overlapping occurrences of the separator in the string. If maxsplit is given, at most maxsplit number of splits occur, and the remainder of the string is returned as the final element of the list (thus, the list will have at most maxsplit+1 elements). If maxsplit is not specified or -1, then there is no limit on the number of splits (all possible splits are made).
The behavior of split on an empty string depends on the value of sep. If sep is not specified, or specified as None, the result will be an empty list. If sep is specified as any string, the result will be a list containing one element which is an empty string.
scanf() production problems in Python
You can use static variables for updating any value inside the function.
class Example:
name = "None"
def printf(text, args):
print(text % args, end = '')
def scanf(text, args):
Example.name = input(text)
scanf("What's your name? ", name)
printf("Hello, %s.\n", Example.name)
There are other pre-build functions to use.
This is the example of the user-defined functions.
If you want to use a pre-defined function. Please let me know.
Working sscanf for Python (preferrably py3k)?
I wrapped the parse module:
from parse import parse
parse("\"%s\"".replace("%s","{}"), "\"test\"").fixed
Related Topics
Convert Structured Array to Regular Numpy Array
How to Open a Website with Urllib via Proxy in Python
Generating File to Download with Django
Using Lxml and Iterparse() to Parse a Big (+- 1Gb) Xml File
How to Assign a Value to a Tensorflow Variable
Comparing Numpy Arrays Containing Nan
Securely Storing Environment Variables in Gae with App.Yaml
Log Output of Multiprocessing.Process
Is It Pythonic to Import Inside Functions
Defining "Boolness" of a Class in Python
Intersection of Two Lists Including Duplicates
Difference Between the Built-In Pow() and Math.Pow() for Floats, in Python
Error Running Basic Tensorflow Example
How to Add Timezone into a Naive Datetime Instance in Python
What Is the Most Compatible Way to Install Python Modules on a MAC
Convert Pandas Dataframe to a Nested Dict