标签:python
自己写的小例子,就是把本地的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 = dict(zip(book_list, date_list))
print type(data)
print data
j = json.dumps(data)
print type(j)
print jwith 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.
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)]
高级应用例子
二维矩阵行列变换
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]]
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]]
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这样也就简化了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
标签:python
原文地址:http://blog.csdn.net/bearkino/article/details/40580729