How to Read a File in Python and Process in a Dictionary

Dict Hash Tabular array

Python'due south efficient cardinal/value hash table construction is called a "dict". The contents of a dict tin can exist written as a serial of primal:value pairs within braces { }, e.g. dict = {key1:value1, key2:value2, ... }. The "empty dict" is only an empty pair of curly braces {}.

Looking up or setting a value in a dict uses square brackets, east.g. dict['foo'] looks upwards the value nether the key 'foo'. Strings, numbers, and tuples piece of work as keys, and whatever type can exist a value. Other types may or may not work correctly equally 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 check if the central is in the dict, or use dict.get(fundamental) which returns the value or None if the key is non present (or get(key, non-plant) allows yous to specify what value to return in the not-institute example).

          ## Can build up a dict by starting with the the empty dict {}   ## and storing key/value pairs into the dict like this:   ## dict[fundamental] = value-for-that-central   dict = {}   dict['a'] = 'alpha'   dict['grand'] = 'gamma'   dict['o'] = 'omega'    print dict  ## {'a': 'blastoff', 'o': 'omega', 'g': 'gamma'}    impress dict['a']     ## Uncomplicated 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: print dict['z']     ## Avoid KeyError   print dict.get('z')  ## None (instead of KeyError)        

dict with keys 'a' 'o' 'g'

A for loop on a lexicon iterates over its keys by default. The keys will announced in an arbitrary order. The methods dict.keys() and dict.values() render lists of the keys or values explicitly. There'due south also an items() which returns a list of (key, value) tuples, which is the well-nigh efficient way to examine all the key value data in the dictionary. All of these lists can be passed to the sorted() function.

          ## By default, iterating over a dict iterates over its keys.   ## Notation that the keys are in a random order.   for key in dict: print fundamental   ## prints a g o      ## Exactly the aforementioned as higher up   for key in dict.keys(): print key    ## Get the .keys() list:   print dict.keys()  ## ['a', 'o', 'g']    ## Likewise, there'south a .values() list of values   impress dict.values()  ## ['blastoff', 'omega', 'gamma']    ## Common example -- loop over the keys in sorted order,   ## accessing each key/value   for key in sorted(dict.keys()):     print fundamental, dict[key]      ## .items() is the dict expressed every bit (key, 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 one (key, value)   ## pair on each iteration.   for k, v in dict.items(): print k, '>', 5   ## a > blastoff    o > omega     g > gamma        

There are "iter" variants of these methods called iterkeys(), itervalues() and iteritems() which avert the price of constructing the whole list -- a performance win if the data is huge. Yet, I by and large prefer the obviously keys() and values() methods with their sensible names. In Python 3 revision, the need for the iterkeys() variants is going abroad.

Strategy note: from a performance signal of view, the dictionary is one of your greatest tools, and you should utilise information technology where you can every bit an like shooting fish in a barrel manner to organize information. For example, you might read a log file where each line begins with an IP accost, and store the data into a dict using the IP address as the key, and the list of lines where it appears every bit the value. Once you've read in the whole file, you lot tin can look upwardly any IP address and instantly see 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 past name:

          hash = {}   hash['word'] = 'garfield'   hash['count'] = 42   s = 'I want %(count)d copies of %(word)s' % hash  # %d for int, %s for string   # 'I want 42 copies of garfield'        

Del

The "del" operator does deletions. In the simplest example, it tin can remove the definition of a variable, as if that variable had not been divers. Del can also be used on list elements or slices to delete that part of the list and to delete entries from a dictionary.

          var = 6   del var  # var no more than!      listing = ['a', 'b', 'c', 'd']   del list[0]     ## Delete start element   del list[-2:]   ## Delete terminal two elements   print listing      ## ['b']    dict = {'a':1, 'b':2, 'c':three}   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 mode. The lawmaking f = open('name', 'r') opens the file into the variable f, gear up for reading operations, and utilise f.shut() when finished. Instead of 'r', employ 'w' for writing, and 'a' for append. The special fashion 'rU' is the "Universal" option for text files where information technology's smart about converting different line-endings so they always come through as a unproblematic '\n'. 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 style to look at all the lines in a text file:

          # Repeat the contents of a file   f = open('foo.txt', 'rU')   for line in f:   ## iterates over the lines of the file     print line,    ## abaft , so impress does not add an terminate-of-line char                    ## since 'line' already includes the end-of-line.   f.close()        

Reading 1 line at a time has the nice quality that not all the file needs to fit in retentivity at one fourth dimension -- handy if you desire to look at every line in a 10 gigabyte file without using ten gigabytes of memory. The f.readlines() method reads the whole file into memory and returns its contents as a list of its lines. The f.read() method reads the whole file into a single cord, which can be a handy style to deal with the text all at once, such as with regular expressions we'll run into later.

For writing, f.write(string) method is the easiest way to write data to an open output file. Or you can use "print" with an open file, but the syntax is nasty: "print >> f, string". In python three, the print syntax volition be fixed to be a regular role call with a file= optional argument: "impress(cord, file=f)".

Files Unicode

The "codecs" module provides support for reading a unicode file.

import codecs  f = codecs.open('foo.txt', 'rU', 'utf-8') for line in f:   # hither line is a *unicode* string        

For writing, employ f.write() since print does non fully support unicode.

Exercise Incremental Development

Building a Python programme, don't write the whole thing in 1 stride. Instead identify just a starting time milestone, e.g. "well the first step is to extract the list of words." Write the code to get to that milestone, and just print your data structures at that indicate, and and then you can do a sys.exit(0) then the program does not run alee into its not-done parts. One time the milestone code is working, you can piece of work on code for the next milestone. Being able to wait at the printout of your variables at i state can help you think about how you need to transform those variables to get to the adjacent state. Python is very quick with this design, allowing you to make a piddling change and run the programme to see how information technology works. Take advantage of that quick turnaround to build your program in little steps.

Exercise: wordcount.py

Combining all the basic Python cloth -- strings, lists, dicts, tuples, files -- try the summary wordcount.py exercise in the Basic Exercises.

perezancentiond.blogspot.com

Source: https://developers.google.com/edu/python/dict-files

0 Response to "How to Read a File in Python and Process in a Dictionary"

إرسال تعليق

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel