For example, in a short text file:
Spoiler:
Moderators: phlip, Moderators General, Prelates
found = 0
for line in file("foo.xml"):
found += line.count("abcd")
print(found)
sum([x.count("abcd") for x in file("foo.xml")])
file("foo.xml").read().count("abcd")
grep -c abcd foo.xml
$ python
Python 2.7.2 (default, Jun 20 2012, 16:23:33)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> f = file("foo.xml")
>>> dir(f)
['__class__', '__delattr__', '__doc__', '__enter__', '__exit__', '__format__', '__getattribute__', '__hash__', '__init__', '__iter__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'close', 'closed', 'encoding', 'errors', 'fileno', 'flush', 'isatty', 'mode', 'name', 'newlines', 'next', 'read', 'readinto', 'readline', 'readlines', 'seek', 'softspace', 'tell', 'truncate', 'write', 'writelines', 'xreadlines']
>>> dir(f.read)
['__call__', '__class__', '__cmp__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__name__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__self__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
>>> print(f.read.__doc__)
read([size]) -> read at most size bytes, returned as a string.
If the size argument is negative or omitted, read until EOF is reached.
Notice that when in non-blocking mode, less data than what was requested
may be returned, even if no size parameter was given.
>>> s = f.read()
>>> dir(s)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>> print(s.count.__doc__)
S.count(sub[, start[, end]]) -> int
Return the number of non-overlapping occurrences of substring sub in
string S[start:end]. Optional arguments start and end are interpreted
as in slice notation.
>>> s.count("abcd")
3
>>> ^D
$
EvanED wrote:Also, help(thingy) is usually easier to use than print(thingy.__doc__), at least IMO. (It gives you a searchable pager -- use / to search.) You could also give ipython a try; I haven't made the switch yet, but looking around it seems like it has some neat features and, if memory serves, one of them is a different & even easier help system.
EvanED wrote:Also, help(thingy) is usually easier to use than print(thingy.__doc__), at least IMO. (It gives you a searchable pager -- use / to search.)
You could also give ipython a try; I haven't made the switch yet, but looking around it seems like it has some neat features and, if memory serves, one of them is a different & even easier help system.
WanderingLinguist wrote:EvanED wrote:Also, help(thingy) is usually easier to use than print(thingy.__doc__), at least IMO. (It gives you a searchable pager -- use / to search.) You could also give ipython a try; I haven't made the switch yet, but looking around it seems like it has some neat features and, if memory serves, one of them is a different & even easier help system.
Wow, cool. I didn't know about help(). Is that something new?
troyp wrote:EvanED wrote:Also, help(thingy) is usually easier to use than print(thingy.__doc__), at least IMO. (It gives you a searchable pager -- use / to search.)
The help system is useful, although tbh, I think it's pretty second-rate. It doesn't even seem to have any error handling. Typing "modules" would crash the help system (and pop up a box asking if I want to quit Idle altogether) because pdfrecycle wasn't handling the "-n" option. I removed pdfrecycle, thinking it might improve things, but it did the opposite. Now when I ask for a module list, Idle hangs irrevocably and has to be killed (presumably because of the misbehaviour of one of the countless other python modules on my Ubuntu system).
Users browsing this forum: wurlitzer153 and 14 guests