练习1:
import json try: d1 = json.loads('{"name":"youlin","type":{"name":"seq", "parameter":["1", "2"]}}') d2 = json.loads('{"name":"qixuan02","type":{"name":"seq","parameter":["1", "2"]}}') except: print 'load json error!!' class Json: def __init__(self): self.json_items = [] def append(self, json_item): self.json_items.append(json_item) return self def __repr__(self): json_dump = '' for item in self.json_items: json_dump += str(item) return json_dump class JsonItem: def __init__(self, reason, expected, actual): self.reason = reason self.expected = expected self.actual = actual def __repr__(self): return 'Reason: {0}\nExpected:{1}\nActual:{2}' \ .format(self.reason, _format_value(self.expected), _format_value(self.actual)) def _indent(s): return '\n'.join(' ' + line for line in s.splitlines()) def _format_value(value): return _indent(json.dumps(value, sort_keys=True, indent=4)) def _is_dict_same(expected, actual, keys): for key in expected: if not key in actual: return False, \ Json().append( JsonItem('Expected key "{0}" Missing from Actual' .format(key), expected, actual)) if not key in keys: # have to change order are_same_flag, json = _are_same(expected[key], actual[key], keys) if not are_same_flag: return False, \ json.append(JsonItem('Different values', expected[key], actual[key])) return True, Json() def _is_list_same(expected, actual, keys): for i in xrange(len(expected)): are_same_flag, json = _are_same(expected[i], actual[i], keys) if not are_same_flag: return False, \ json.append( JsonItem('Different values', expected[i], actual[i])) return True, Json() def _are_same(expected, actual, keys): if expected is None: return expected == actual, Json() # Ensure they are of same type if type(expected) != type(actual): return False, \ Json().append( JsonItem('Type Mismatch: Expected Type: {0}, Actual Type: {1}' .format(type(expected), type(actual)), expected, actual)) # Compare primitive types immediately if type(expected) in (int, str, bool, long, float, unicode): return expected == actual, Json() if len(expected) != len(actual): return False, \ Json().append( JsonItem('Length Mismatch: Expected Length: {0}, Actual Length: {1}' .format(len(expected), len(actual)), expected, actual)) if isinstance(expected, dict): return _is_dict_same(expected, actual, keys) if isinstance(expected, list): return _is_list_same(expected, actual, keys) return False, Json().append(JsonItem('Unhandled Type: {0}' .format(type(expected)), expected, actual)) def json_are_same(a, b, keys=[]): return _are_same(a, b, keys) print json_are_same(a1, a2, [])
cat test.txt | awk -F '&luw' 'BEGIN {print "[BEGIN PARSE..]" } { wordstr=substr($2, 2); split(wordstr, words, "#"); channelstr=substr($3, 3); wordstr=substr($2, 2); split(wordstr, words, "#"); split($7, temp_arr, "&luctr:"); split(temp_arr[2], temp_arr2, "&lumidcpm:"); split(temp_arr2[1], luctrs, "#"); times[1]=0; times[2]=0; times[3]=0; times[4]=0; times[5]=0; times[6]=0; times[7]=0; times[8]=0; times[9]=0; for (i in words) { channel=substr(channelstr, i, 1); times[channel]++; print "日志行数 "NR" 单词:"words[i]" 通道:"channel" pctr:"luctrs[i]; } } END { for(i=1; i<10; i++) { print "Channel "i" show "times[i]" times"; } }'