首页 > 代码库 > Python interview - text to JSON & zip & with statement

Python interview - text to JSON & zip & with statement

自己写的小例子,就是把本地的txt文件转换为JSON格式输出。


data source

简单的1-10存一个文件, A-J 存一个文件


import json

book = 'C:\Python27\\book.txt'
date = 'C:\Python27\date.txt'
book_list = []
date_list = []

with open(book) as b:
    book_list = b.read().splitlines()

with open(date) as d:
    date_list = d.read().splitlines()

data = http://www.mamicode.com/dict(zip(book_list, date_list))>
很简单的先从本地中读取文件,然后用with关键词配合open方法打开文件。读取文件的时候忽略‘\n‘,不然会在最终结果出现一对‘\n‘:‘\n’。

with open(fname) as f:
    content = f.readlines()
会得到‘\n‘

with open(fname) as f:
    content = f.read().splitlines()
不会得到‘\n‘

然后我们将两个list用zip方法组合,转换为dict字典类型,再用json.dumps方法转换为JSON格式输出。


<type 'dict'>
{'A': '1', 'C': '3', 'B': '2', 'E': '5', 'D': '4', 'G': '7', 'F': '6', 'I': '9', 'H': '8', 'J': '10'}
<type 'str'>
{"A": "1", "C": "3", "B": "2", "E": "5", "D": "4", "G": "7", "F": "6", "I": "9", "H": "8", "J": "10"}


####################################################################################################################################


zip()

首先我们看help(zip)


zip(...)
    zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]
    
    Return a list of tuples, where each tuple contains the i-th element
    from each of the argument sequences.  The returned list is truncated
    in length to the length of the shortest argument sequence.

zip接受一系列可迭代对象作为参数,讲对象中对应的元素打包成一个个tuple,然后返回由这些tuples组成的list。P.S. 如果传入的参数不等长,那么返回的list的长度和参数中长度最短的对象相同。


z1=[1,2,3]
z2=[4,5,6]
result = zip(z1,z2)
print result
z3=[4,5,6,7]
result = zip(z1,z3)
print result
result = zip(*result)
print result
# result

[(1, 4), (2, 5), (3, 6)]
[(1, 4), (2, 5), (3, 6)]
[(1, 2, 3), (4, 5, 6)]

配合*星号操作符,我们可以把zip的列表再解压,把初始的两个list还原出来以tuple形式展现。


高级应用例子

二维矩阵行列变换

a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

print [[row[col] for row in a] for col in range(len(a[0]))]

# result

[[1, 4, 7], [2, 5, 8], [3, 6, 9]]

或者利用zip配合map可以达到同样的效果

a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

print zip(*a)
print map(list,zip(*a))

# result

[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]

使用zip反转字典

m = {'a': 1, 'b': 2, 'c': 3, 'd': 4}

print m.items()
print zip(m.values(), m.keys())

mi = dict(zip(m.values(), m.keys()))
print m
print mi

# result

[('a', 1), ('c', 3), ('b', 2), ('d', 4)]
[(1, 'a'), (3, 'c'), (2, 'b'), (4, 'd')]
{'a': 1, 'c': 3, 'b': 2, 'd': 4}
{1: 'a', 2: 'b', 3: 'c', 4: 'd'}


with statement

with的实质是一个控制流语句,可以用来简化try-finally语句。主要功能是实现一个类__enter__()和__exit__()方法

class controlled_execution:
    def _enter__(self):
        set things up
        return thing
    def __exit__(self, type, value,  traceback):
        tear thing down
with controlled_execution() as thing:
    some code

在实际的运行过程中,首先运行enter里面的代码,返回thing,作为as之后的变量值,然后再与运行with模块中的代码,最后会自动执行exit中的代码,不论with中的代码运行结果如何。

这样也就简化了try-finally语句。同样用在读取文件的操作中,将文件的关闭操作放在exit方法中,就不会忘记释放文件而出现错误。

P.S. exit方法的返回值可以用来指示with部分的代码出现的异常是否需要raise,如果返回false,则会raise,否则,不进行操作。


此外,python库中有contextlib模块,使你不用构造含有__enter__和__exit__方法的类就可以使用with

>>> from contextlib import contextmanager  
>>> from __future__ import with_statement  
>>> @contextmanager  
... def context():  
...     print 'entering the zone'  
...     try:  
...         yield  
...     except Exception, e:  
...         print 'with an error %s'%e  
...         raise e  
...     else:  
...         print 'with no error'  
...  
>>> with context():  
...     print '----in context call------'  
...  
entering the zone  
----in context call------  
with no error  

from contextlib import closing  
import urllib  
  
with closing(urllib.urlopen('http://www.python.org')) as page:  
    for line in page:  
        print line  


P.S. 在一个with后面,你可以open多个文件,比如

def filter(txt, oldfile, newfile):
    '''    Read a list of names from a file line by line into an output file.
    If a line begins with a particular name, insert a string of text
    after the name before appending the line to the output file.
    '''

    with open(newfile, 'w') as outfile, open(oldfile, 'r', encoding='utf-8') as infile:
        for line in infile:
            if line.startswith(txt):
                line = line[0:len(txt)] + ' - Truly a great person!\n'
            outfile.write(line)

# input the name you want to check against
text = input('Please enter the name of a great person: ')    
letsgo = filter(text,'Spanish', 'Spanish2')




参考:

http://www.cnblogs.com/diyunpeng/archive/2011/09/15/2177028.html 

http://blog.csdn.net/largetalk/article/details/6910277

http://www.cnblogs.com/Brogrammer/archive/2012/07/23/2605570.html

http://effbot.org/zone/python-with-statement.htm

Python interview - text to JSON & zip & with statement