1. operation and an array of json json object
in python, a Json objects it contains a lot of information, how to get what we want from these data?
Before the
data types into a format json python object
article has been described how to operate a json object herein is also common to add a process Json array.
the python json module has two main functions.
1.python object into json format:
json.dumps ()
2. json data into the python objects:
json.loads ()
is:
. 1 Import json 2 json.dumps () # string into the dictionary or list format josn . 3 json.loads () # json format string into the python objects . 4 json.dump () # into the dictionary or list json string and written to the file . 5 the json.load () # json format string read from a file into an object python
In short remember:
(with s are operating on the string, without the s is to txt file the class action.)
Do not understand, with examples:
Source:
1 "" " 2 a, first part: mutual conversion Json format python object . 3 " " ' . 4 . 5 Import json . 6 . 7 # Common json single list . 8 jsonData = ' '' . 9 { 10 " Uin ": 0 , . 11 "UserName": "@ c482d142bc698bc3971d9f8c26335c5c" , 12 is "the NickName": "CSDNzoutao" , 13 is "HeadImgUrl": "https://blog.csdn.net/ITBigGod" , 14 "the DisplayName": "ZouTao" , 15 "ChatRoomId ":0, 16 "KeyWord":"che", 17 "EncryChatRoomId": "" , 18 is "isOwner": 0 . 19 } 20 is ' '' 21 is 22 is myfriend = json.loads (jsonData) # JSON conversion dictionary object 23 is Print (myfriend) 24 later turned # dictionary, according to key to obtain the values for the various fields 25 name = myfriend.get ( 'the NickName' ) 26 is Print (name) 27 # json.dumps (name) # object into the python JSON 28 29 # Json array there are common - nested type 30 Json_doc = '' ' 31 is { 32 "memberList" : [{ 33 is "UserName": "CSDNzoutao", 34 is "Sex": "M" , 35 "Age": 10 36 }, 37 [ { 38 is "UserName": "CSDNzoutao girlfriend No. 1" , 39 "Sex": "female" , 40 "Age": 10 41 is }, 42 is { 43 is "UserName": "CSDNzoutao girlfriend No. 2" , 44 is "Sex": "not" , 45 "Age": 10 46 }] 47 } 48 ''' 49 50 myfriends = json.loads (Json_doc) 51 is memberList = myfriends.get ( 'memberList') to obtain a list object # - contains dictionary data 52 is Print (memberList) 53 is 54 is # with a for loop can easily obtain the data 55 for X in memberList: 56 is Print ( 'traversal list data:', x)
Icon:
2.python read and write all kinds of txt, csv, html, xls file tools
In the above objects python json format string and input data memberList.
python read and write all kinds of txt, csv, html, xls file:
Source:
13 # a + is a mode because of the additional mode, the pointer has moved to the end of the text, it is read out of an empty string. 14 or FText if not = f.read () # read one time to a string of all 15 ftextlist = f.readlines () # read all disposable, but each line is stored as a list of clause 16 fLINE = f.readline () or just # reads one line 17 Print (ftextlist, fLINE) 18 f.close () # close the file 19 ## summary: after using both read and write files with open statements, do not, as with f = open () this statement like before ! 20 is 21 is 22 is # saved as an html file 23 is DEF output_html (memberList): 24 Print (type (memberList)) # < class ' List '> 25 FOUT = Open (' and result.html ',' W ', encoding =' UTF -8 ' ) 26 is fout.write (' <HTML> ' ) 27 fout.write('<meta charset=utf-8') 28 fout.write('<body>') 29 fout.write('<table>') 30 31 # Python 默认编码格式是: Ascii 32 for data in memberList: # result=list,data={} 33 fout.write('<tr>') 34 fout.write('<hr>') 35 fout.write('<td>%s</td>' % data['UserName']) 36 fout.write('<td>%s</td>' % data['Sex']) 37 fout.write('<hr>') 38 fout.write('</tr>') 39 fout.write ( '</ Table>' ) 40 fout.write ( '</ body>' ) 41 is fout.write ( '</ HTML>' ) 42 is fout.close () 43 is 44 is # csv file is saved as 45 output_csv DEF (DataList): 46 is Print (type (DataList)) # < class 'List'> 47 Import csv 48 # ready to store data csv file 49 CSV_File = Open ( "the Result.csv", 'W', NEWLINE = '', encoding = 'UTF-8-SIG' ) to solve the Chinese garbled # 50 Writer = CSV. Writer (CSV_File) 51 is writer.writerow ([ 'a column heading,' 'column headings two', 'three column heading']) 52 for data in datalist: 53 writer.writerow([data['UserName'], data['Sex'],data['Age']]) 54 csv_file.close() 55 56 #用reder读取csv文件 57 with open('result.csv','r') as csvFile: 58 reader = csv.reader(csvFile) 59 for line in reader: 60 print(line) 61 62 # 保存为xls文件 63 def output_xls(datalist): 64 print(type(datalist)) # <class 'list'> 65 import xlwt 66 ## Creating Workbooks 67 Workbook = xlwt.Workbook (encoding = 'UTF-8' ) 68 # Create Sheet 69 Sheet = workbook.add_sheet (u'sheet1 ', cell_overwrite_ok = True) 70 head = [' name ',' sex ',' age ' ] # define the header 71 is # worksheet.write () function name written in the first row, the parameters represent the row, column, data, data format. 72 for H in Range (len (head)): 73 is sheet.write (0 , H, head [H]) # 0 generates a first row headings 74 75 # fill data (row 1 begins to fill the number 0) 76 Row 1 = 77 for Product in the DataList: a list kept inside a dictionary # 78 sheet.write (Row, 0, Product [ 'UserName' ]) 79 sheet.write (Row,. 1, Product [ 'Sex' ]) 80 sheet.write (Row, 2, Product [ 'Age' ]) 81 Row + . 1 = 82 workbook.save ( 'result.xls' ) 83 84 # data read excel 85 Import to xlrd 86 data = xlrd.open_workbook ( 'result.xls' ) open xls file # 87 Table data.sheets = () [0 ] open the first table # 88 nrows = table.nrows # Get the number of rows of the table 89 for I in Range (nrows): progressive printing cycle # 90 IF0 == I : Skip the first line # 91 is Continue 92 Print (table.row_values (I)) 93 94 95 # exemplary call 96 IF the __name__ == '__main__' : 97 98 # memberList is a list, which is embedded dictionary 99 output_html (memberList) 100 output_csv (memberList) 101 output_xls (memberList) 102 103 for Result in memberList: 104 #Print ( 'final filtered data is:' , Result) 105 the output_txt (Result) # generator object is out of a traverse dictionary.
Data generated by various types of files:
remind! ! ! Later, when read txt file, use with open statements, do not, as with f = open as before (this statement) up!