Read Lines From a Log File and Print in Html in Python
Dict Hash Table
Python's efficient key/value hash table structure is called a "dict". The contents of a dict can be written every bit a serial of primal:value pairs within braces { }, due east.g. dict = {key1:value1, key2:value2, ... }. The "empty dict" is just an empty pair of curly braces {}.
Looking upward or setting a value in a dict uses foursquare brackets, eastward.chiliad. dict['foo'] looks up the value nether the key 'foo'. Strings, numbers, and tuples work every bit keys, and any blazon can be a value. Other types may or may not work correctly as keys (strings and tuples work cleanly since they are immutable). Looking up a value which is not in the dict throws a KeyError -- use "in" to bank check if the key is in the dict, or employ dict.become(key) which returns the value or None if the key is not nowadays (or get(primal, non-establish) allows you lot to specify what value to return in the non-found case).
## Tin can build up a dict by starting with the the empty dict {} ## and storing central/value pairs into the dict like this: ## dict[key] = value-for-that-key dict = {} dict['a'] = 'alpha' dict['grand'] = 'gamma' dict['o'] = 'omega' print dict ## {'a': 'alpha', 'o': 'omega', 'grand': 'gamma'} print dict['a'] ## Unproblematic lookup, returns 'alpha' dict['a'] = 6 ## Put new key/value into dict 'a' in dict ## True ## print dict['z'] ## Throws KeyError if 'z' in dict: impress dict['z'] ## Avert KeyError impress dict.get('z') ## None (instead of KeyError)
A for loop on a dictionary iterates over its keys by default. The keys will announced in an arbitrary social club. The methods dict.keys() and dict.values() return lists of the keys or values explicitly. There'south also an items() which returns a list of (cardinal, value) tuples, which is the about efficient way to examine all the key value data in the lexicon. All of these lists tin be passed to the sorted() part.
## By default, iterating over a dict iterates over its keys. ## Annotation that the keys are in a random club. for key in dict: print key ## prints a g o ## Exactly the same as above for key in dict.keys(): impress cardinal ## Become the .keys() list: print dict.keys() ## ['a', 'o', 'grand'] ## Likewise, there's a .values() list of values print dict.values() ## ['alpha', 'omega', 'gamma'] ## Mutual case -- loop over the keys in sorted order, ## accessing each central/value for key in sorted(dict.keys()): print key, dict[central] ## .items() is the dict expressed as (fundamental, value) tuples print dict.items() ## [('a', 'alpha'), ('o', 'omega'), ('g', 'gamma')] ## This loop syntax accesses the whole dict past looping ## over the .items() tuple list, accessing i (key, value) ## pair on each iteration. for k, v in dict.items(): print chiliad, '>', v ## a > alpha o > omega thou > gamma
In that location are "iter" variants of these methods chosen iterkeys(), itervalues() and iteritems() which avoid the price of constructing the whole listing -- a performance win if the data is huge. However, I generally adopt the plain keys() and values() methods with their sensible names. In Python 3 revision, the demand for the iterkeys() variants is going away.
Strategy notation: from a performance indicate of view, the dictionary is one of your greatest tools, and you should apply it where you lot tin can as an easy way to organize information. For example, y'all might read a log file where each line begins with an IP accost, and store the information into a dict using the IP address as the primal, and the listing of lines where it appears every bit the value. One time you lot've read in the whole file, yous tin await upward whatsoever IP address and instantly meet its list of lines. The lexicon takes in scattered data and makes it into something coherent.
Dict Formatting
The % operator works conveniently to substitute values from a dict into a string by name:
hash = {} hash['give-and-take'] = 'garfield' hash['count'] = 42 south = 'I want %(count)d copies of %(word)s' % hash # %d for int, %due south for cord # 'I want 42 copies of garfield'
Del
The "del" operator does deletions. In the simplest case, it can remove the definition of a variable, as if that variable had non been defined. Del can also be used on listing elements or slices to delete that part of the list and to delete entries from a lexicon.
var = 6 del var # var no more! list = ['a', 'b', 'c', 'd'] del listing[0] ## Delete kickoff element del list[-ii:] ## Delete concluding ii elements print list ## ['b'] dict = {'a':one, 'b':2, 'c':3} del dict['b'] ## Delete 'b' entry print dict ## {'a':1, 'c':iii}
Files
The open() function opens and returns a file handle that tin can be used to read or write a file in the usual way. The code f = open up('proper name', 'r') opens the file into the variable f, fix for reading operations, and use f.shut() when finished. Instead of 'r', use 'w' for writing, and 'a' for append. The special mode 'rU' is the "Universal" option for text files where it's smart nigh converting different line-endings and then they always come through every bit a simple '\north'. The standard for-loop works for text files, iterating through the lines of the file (this works but for text files, not binary files). The for-loop technique is a simple and efficient way to look at all the lines in a text file:
# Echo the contents of a file f = open up('foo.txt', 'rU') for line in f: ## iterates over the lines of the file print line, ## trailing , so impress does not add an end-of-line char ## since 'line' already includes the cease-of-line. f.close()
Reading ane line at a fourth dimension has the prissy quality that non all the file needs to fit in memory at 1 time -- handy if you want to look at every line in a 10 gigabyte file without using 10 gigabytes of retentiveness. The f.readlines() method reads the whole file into memory and returns its contents every bit a list of its lines. The f.read() method reads the whole file into a single string, which tin can exist a handy way to deal with the text all at once, such as with regular expressions we'll run into subsequently.
For writing, f.write(cord) method is the easiest style to write data to an open up output file. Or you can use "print" with an open file, but the syntax is nasty: "print >> f, cord". In python iii, the impress syntax volition exist stock-still to be a regular office call with a file= optional statement: "print(string, file=f)".
Files Unicode
The "codecs" module provides support for reading a unicode file.
import codecs f = codecs.open up('foo.txt', 'rU', 'utf-8') for line in f: # here line is a *unicode* cord
For writing, use f.write() since impress does not fully support unicode.
Exercise Incremental Evolution
Edifice a Python program, don't write the whole matter in one step. Instead identify simply a first milestone, east.g. "well the starting time step is to extract the list of words." Write the code to get to that milestone, and but print your information structures at that betoken, so you lot tin can practise a sys.go out(0) so the programme does non run ahead into its not-done parts. Once the milestone code is working, you can work on code for the side by side milestone. Being able to look at the printout of your variables at ane state tin assistance you think most how y'all need to transform those variables to get to the next state. Python is very quick with this pattern, assuasive you to brand a little change and run the program to see how it works. Have advantage of that quick turnaround to build your program in little steps.
Practise: wordcount.py
Combining all the basic Python material -- strings, lists, dicts, tuples, files -- attempt the summary wordcount.py exercise in the Basic Exercises.
Read Lines From a Log File and Print in Html in Python
Source: https://developers.google.com/edu/python/dict-files
0 Response to "Read Lines From a Log File and Print in Html in Python"
Post a Comment