Python built-in function (a)

 Our experiments using Python code to parse the following files:

first	last	email
john	smith	[email protected]
jane	doan	[email protected]
david	neilson	[email protected]

 First we enter the command line with a file name read, extract the header information of the file by readline function. Then for ...... in ....... read line by line text message: a reading process, a zip function header information and corresponding table items packaged, the results obtained similarly [( 'first', 'john') ( 'last', 'smith') ( 'email', '[email protected]')]. Dict last constructor takes a list, the first element in the list of each tuple keys of a dictionary, the dictionary as the value of the second element, and the result is added to the list, and print it out.

import sys
filename = sys.argv[1]
contacts = []
with open(filename) as file:
    header = file.readline().strip().split()
    for line in file:
        line = line.strip().split()
        content = zip(header, line)
        contacts.append(dict(content))
    for contact in contacts:
        print("email: {email} -- {first}, {last}".format(**contact))

If you use a list in Python parsing, then the above code can be more concise, but relatively speaking, affect the readability of the code:

import sys
filename = sys.argv[1]
contacts = []
with open(filename) as file:
    header = file.readline().strip().split()
    contacts = [ dict(zip(header,line.strip().split())) for line in file]
    for contact in contacts:
        print("email: {email} -- {first}, {last}".format(**contact))


Additional information:

Function strip can be removed using the tab end of the text, line breaks, and in accordance with the split function is divided whitespace text (default) returns a list of the final object.

Published 98 original articles · won praise 5 · views 10000 +

Guess you like

Origin blog.csdn.net/chengsilin666/article/details/84825464