文件操作–Day2
1.读取整个文件
with open ('./Desktop/test_json_data.json') as file: #要特别注意文件的路径./当前目录下
contents = file.read()
print((contents).strip()) #read()到达文件末尾时会返回一个空字符串,显示出来就是一个空行,所以要交strip()函数删除空行
[
{
"id": 0,
"isActive": true,
"balance": "1,957.88",
"age": 23,
"username": "Alta",
"email": "altamckee@zensus.com",
"phone": "+1 (898) 593-3470",
"registered": "2020-06-05T08:12:50 -08:00",
"latitude": 55.067808,
"longitude": -126.171323,
"tags": [
"proident",
"ipsum",
"elit",
"esse",
"pariatur",
"labore",
"nisi"
......
......
"id": 75,
"isActive": false,
"balance": "1,274.47",
"age": 33,
"username": "Irwin",
"email": "irwinmckee@zensus.com",
"phone": "+1 (946) 521-3313",
"registered": "2019-09-07T12:48:45 -08:00",
"latitude": -47.913944,
"longitude": -40.715072,
"tags": [
"esse",
"aliquip",
"sit",
"ex",
"occaecat",
"Lorem",
"consequat"
],
"greeting": "Hello, undefined! You have 1 unread messages.",
"favoriteFruit": "strawberry"
}
]
#要让Python打开与程序文件不位于同一个目录中的文件,需要提供文件路径,在Windows系统中,在文件路径中使用反斜杠(\ )而不是斜杠(/ ):
- 逐行读取
#要以每次一行的方式检查文件,可对文件对象使用for 循环
for line in contents:
print(line.strip()) #使用一个简单的for 循环来打印lines 中的各行
或者
lines = file.readlines() #从文件中读取每一行,并将其存储在一个列表中
- 使用文件内容
file_string = '' #创建变量file_string存储文件contents内容
for line in contents:
file_string +=line.strip() #用循环将各行都加入file_string并删除每行末尾换行符
print(file_string)
[{"id":0,"isActive":true,"balance":"1,957.88","age":23,"username":"Alta","email":"altamckee@zensus.com","phone":"+1(898)593-3470","registered":"2020-06-05T08:12:50-08:00","latitude":55.067808,"longitude":-126.171323,"tags":["proident","ipsum","elit","esse","pariatur","labore","nisi"],"greeting":"Hello,undefined!Youhave6unreadmessages.","favoriteFruit":"banana"},{"id":1,"isActive":true,"balance":"1,591.20","age":40,"username":"Lilian","email":"lilianmckee@zensus.com","phone":"+1(872)565-3204","registered":"2022-01-26T09:50:15-08:00","latitude":5.55564,"longitude":-14.003746,"tags":["id","cillum","elit","ad","est","velit","tempor"],"greeting":"Hello,undefined!Youhave9unreadmessages.","favoriteFruit":"apple"},{"id":2,"isActive":false,"balance":"2,538.80","age":30,"username":"Frank","email":"frankmckee@zensus.com","phone":"+1(849)506-3365","registered":"2016-09-18T11:05:30-08:00","latitude":2.81869,"longitude":128.647055,"tags":["excepteur","ullamco","reprehenderit","irure","culpa","occaecat","ullamco"],"greeting":"Hello,undefined!Youhave1unreadmessages.","favoriteFruit":"apple"},{"id":3,"balance":"3,561.54","age":22,"username":"Paula","email":"paulamckee@zensus.com","phone":"+1(978)462-2884","registered":"2020-10-24T02:07:08-08:00","latitude":74.538512,"longitude":101.372685,"tags":["adipisicing","incididunt","minim","culpa","incididunt","sit","ad"],"greeting":"Hello,undefined!Youhave9unreadmessages.","favoriteFruit":"apple"},{"id":4,"isActive":true,"balance":"1,141.44","age":40,"username":"Laura","email":"lauramckee@zensus.com","phone":"+1(970)430-3691","registered":"2014-05-31T09:28:16-08:00","latitude":64.765994,"longitude":6.237858,"tags":["nulla","officia","enim","ullamco","do","dolor","mollit"],"greeting":"Hello,undefined!Youhave9unreadmessages.","favoriteFruit":"strawberry"},{"id":5,"isActive":false,"balance":"3,475.50","age":35,"username":"Boyer","email":"boyermckee@zensus.com","phone":"+1(806)520-2707","registered":"2019-03-14T03:36:01-08:00","latitude":64.697295,"longitude":-10.047144,"tags":["excepteur","elit","et","mollit","nostrud","consequat","adipisicing"],"greeting":"Hello,undefined!Youhave5unreadmessages.","favoriteFruit":"banana"},{"id":6,"isActive":true,"balance":"1,724.26","age":28,"username":"Gay","email":"gaymckee@zensus.com","phone":"+1(821)505-3033","registered":"2015-04-24T03:46:47-08:00","latitude":-74.777288,"longitude":104.977168,"tags":["labore","laboris","tempor","velit","est","qui","ipsum"],"greeting":"Hello,undefined!Youhave8unreadmessages.","favoriteFruit":"apple"},{"id":7,"isActive":true,"balance":"3,134.76","age":39,"username":"Marlene","email":"marlenemckee@zensus.com","phone":"+1(951)563-3448","registered":"2019-03-02T12:09:10-08:00","latitude":54.001815,"longitude":81.353144,"tags":["do","nostrud","cupidatat","aliquip","pariatur","sint","aliquip"],"greeting":"Hello,undefined!Youhave8unreadmessages.","favoriteFruit":"banana"},{"id":8,"isActive":true,"balance":"3,907.32","age":20,"username":"Mayra","email":"mayramckee@zensus.com","phone":"+1(850)470-2965","registered":"2014-06-15T07:04:58-08:00","latitude":-72.784577,"longitude":-86.005952,"tags":["dolor","et","esse","velit","sit","irure","aliqua"],"greeting":"Hello,undefined!Youhave7unreadmessages.","favoriteFruit":"strawberry"},{"id":9,"isActive":false,"balance":"2,347.74","age":40,"username":"Horton","email":"hortonmckee@zensus.com","phone":"+1(831)459-2644","registered":"2020-11-18T03:29:09-08:00","latitude":2.928268,"longitude":-13.225237,"tags":["pariatur","consectetur","et","ullamco","qui","Lorem","minim"],"greeting":"Hello,undefined!Youhave1unreadmessages.","favoriteFruit":"apple"},{"id":10,"isActive":true,"balance":"2,365.19","age":39,"username":"Aisha","email":"aishamckee@zensus.com","phone":"+1(817)597-2863","registered":"2020-04-29T01:55:41-08:00","latitude":-45.308095,"longitude":149.528702,"tags":["quis","deserunt","ad","est","eiusmod","voluptate","eu"],"greeting":"Hello,undefined!Youhave9unreadmessages.","favoriteFruit":"apple"},{"id":11,"isActive":false,"balance":"1,215.03","age":36,"username":"Butler","email":"butlermckee@zensus.com","phone":"+1(888)448-2583","registered":"2021-02-09T02:58:52-08:00","latitude":-88.059546,"longitude":-18.318412,"tags":["laborum","nisi","et","aliquip","ea","nostrud","eu"],"greeting":"Hello,undefined!Youhave10unreadmessages.","favoriteFruit":"banana"},{"id":12,"isActive":true,"balance":"3,388.41","age":22,"username":"Julianne","email":"juliannemckee@zensus.com","phone":"+1(866)575-2771","registered":"2019-07-19T03:12:55-08:00","latitude":-11.36204,"longitude":124.72747,"tags":["qui","duis","ad","elit","occaecat","culpa","aliquip"],"greeting":"Hello,undefined!Youhave8unreadmessages.","favoriteFruit":"banana"},{"id":13,"isActive":true,"balance":"2,590.43","age":22,"username":"Lucile","email":"lucilemckee@zensus.com","phone":"+1(806)428-2761","registered":"2021-07-20T10:19:07-08:00","latitude":-73.754497,"longitude":-107.050303,"tags":["magna","mollit","cillum","id","aute","excepteur","et"],"greeting":"Hello,undefined!Youhave3unreadmessages.","favoriteFruit":"banana"},{"id":14,"isActive":false,"balance":"3,029.17","age":"33","username":"Cross","email":"crossmckee@zensus.com","phone":"+1(835)561-3823","registered":"2014-07-26T03:19:57-08:00","latitude":80.212593,"longitude":20.497338,"tags":["sint","Lorem","sit","id","elit","consequat","adipisicing"],"greeting":"Hello,undefined!Youhave4unreadmessages.","favoriteFruit":"banana"},{"id":15,"isActive":false,"balance":"3,465.10","age":26,"username":"Hayes","email":"hayesmckee@zensus.com","phone":"+1(858)418-2994","registered":"2021-09-21T06:13:59-08:00","latitude":35.58498,"longitude":166.962556,"tags":["quis","eu","aliqua","incididunt","culpa","incididunt","ipsum"],"greeting":"Hello,undefined!Youhave7unreadmessages.","favoriteFruit":"strawberry"},{"id":16,"isActive":false,"balance":"2,866.15","age":28,"username":"Valentine","email":"valentinemckee@zensus.com","phone":"+1(972)597-3027","registered":"2014-01-03T01:55:15-08:00","latitude":-33.369981,"longitude":169.125766,"tags":["nulla","officia","laborum","officia","fugiat","elit","deserunt"],"greeting":"Hello,undefined!Youhave7unreadmessages.","favoriteFruit":"banana"},{"id":17,"isActive":false,"balance":"3,465.57","age":26,"username":"Weiss","email":"weissmckee@zensus.com","phone":"+1(805)451-3175","registered":"2020-08-21T06:34:59-08:00","latitude":-46.299543,"longitude":-28.604967,"tags":["voluptate","Lorem","quis","ullamco","pariatur","anim","nulla"],"greeting":"Hello,undefined!Youhave5unreadmessages.","favoriteFruit":"strawberry"},{"id":18,"isActive":true,"balance":"1,504.14","age":21,"username":"Gwendolyn","email":"gwendolynmckee@zensus.com","phone":"+1(923)489-2289","registered":"2021-11-10T12:11:48-08:00","latitude":-12.737915,"longitude":-102.10785,"tags":["ullamco","tempor","Lorem","velit","aliqua","ad","consequat"],"greeting":"Hello,undefined!Youhave10unreadmessages.","favoriteFruit":"banana"},{"id":19,"isActive":false,"balance":"2,386.82","age":24,"username":"Effie","email":"effiemckee@zensus.com","phone":"+1(878)588-2853","registered":"2018-08-29T02:24:18-08:00","latitude":70.833282,"longitude":-151.771232,"tags":["sit","proident","anim","culpa","aliqua","consequat","Lorem"],"greeting":"Hello,undefined!Youhave4unreadmessages.","favoriteFruit":"apple"},{"id":20,"isActive":true,"balance":"3,886.90","age":23,"username":"Adams","email":"adamsmckee@zensus.com","phone":"+1(994)485-2227","registered":"2020-11-30T04:01:02-08:00","latitude":44.00537,"longitude":-124.988102,"tags":["veniam","ipsum","labore","duis","eiusmod","do","magna"],"greeting":"Hello,undefined!Youhave10unreadmessages.","favoriteFruit":"strawberry"},{"id":21,"isActive":true,"balance":"2,238.56","age":23,"username":"Rice","email":"ricemckee@zensus.com","phone":"+1(903)555-3429","registered":"2015-02-24T12:45:15-08:00","latitude":34.662514,"longitude":-143.133505,"tags":["adipisicing","duis","elit","aute","sit","voluptate","consequat"],"greeting":"Hello,undefined!Youhave3unreadmessages.","favoriteFruit":"apple"},{"id":22,"isActive":false,"balance":"1,291.01","age":23,"username":"Bonner","email":"bonnermckee@zensus.com","phone":"+1(988)493-2700","registered":"2014-06-01T07:52:21-08:00","latitude":70.957043,"longitude":-176.54459,"tags":["ad","velit","ad","non","exercitation","laboris","consectetur"],"greeting":"Hello,undefined!Youhave3unreadmessages.","favoriteFruit":"banana"},{"id":23,"isActive":false,"balance":"1,632.91","age":31,"username":"Lisa","email":"lisamckee@zensus.com","phone":"+1(986)412-2109","registered":"2017-06-17T06:39:12-08:00","latitude":-67.744103,"longitude":70.840212,"tags":["Lorem","laborum","labore","ea","quis","non","adipisicing"],"greeting":"Hello,undefined!Youhave6unreadmessages.","favoriteFruit":"apple"},{"id":24,"isActive":false,"balance":"1,865.56","age":24,"username":"Rae","email":"raemckee@zensus.com","phone":"+1(812)463-2528","registered":"2016-02-14","latitude":-33.410055,"longitude":99.174982,"tags":["amet","nostrud","qui","tempor","eiusmod","aliqua","irure"],"greeting":"Hello,undefined!Youhave4unreadmessages.","favoriteFruit":"banana"},{"id":25,"isActive":true,"balance":"2,601.47","age":32,"username":"Graham","email":"grahammckee@zensus.com","phone":"+1(931)492-2595","registered":"2020-05-09T01:03:45-08:00","latitude":-72.266549,"longitude":-66.621125,"tags":["amet","officia","Lorem","culpa","culpa","reprehenderit","et"],"greeting":"Hello,undefined!Youhave9unreadmessages.","favoriteFruit":"banana"},{"id":26,"isActive":true,"balance":"2,636.52","age":24,"username":"Pat","email":"patmckee@zensus.com","phone":"+1(931)402-3655","registered":"2022-02-15T05:13:02-08:00","latitude":35.061864,"longitude":-147.882429,"tags":["quis","minim","aute","commodo","sint","sint","id"],"greeting":"Hello,undefined!Youhave3unreadmessages.","favoriteFruit":"strawberry"},{"id":27,"isActive":true,"balance":"3,151.01","age":28,"username":"Teri","email":"terimckee@zensus.com","phone":"+1(877)472-3965","registered":"2015-12-30T10:39:53-08:00","latitude":-75.313044,"longitude":-35.944372,"tags":["eu","reprehenderit","ad","ex","excepteur","nulla","amet"],"greeting":"Hello,undefined!Youhave7unreadmessages.","favoriteFruit":"apple"},{"id":28,"isActive":false,"balance":"1,434.27","age":37,"username":"Alvarez","email":"alvarezmckee@zensus.com","phone":"+1(956)553-2543","registered":"2016-09-07T11:36:01-08:00","latitude":-7.298283,"longitude":16.037206,"tags":["velit","eu","consectetur","laborum","proident","minim","mollit"],"greeting":"Hello,undefined!Youhave8unreadmessages.","favoriteFruit":"strawberry"},{"id":29,"isActive":true,"balance":"1,448.50","age":36,"username":"Glenda","email":"glendamckee@zensus.com","phone":"+1(845)571-2665","registered":"2014-01-21T11:53:44-08:00","latitude":65.268129,"longitude":-171.211242,"tags":["ad","cupidatat","labore","tempor","id","anim","ullamco"],"greeting":"Hello,undefined!Youhave3unreadmessages.","favoriteFruit":"strawberry"},{"id":30,"isActive":true,"balance":"3,806.19","age":33,"username":"Anderson","email":"andersonmckee@zensus.com","phone":"+1(931)562-3707","registered":"2015-07-10T10:40:28-08:00","latitude":-61.587878,"longitude":158.539182,"tags":["sit","minim","qui","consectetur","enim","duis","consequat"],"greeting":"Hello,undefined!Youhave10unreadmessages.","favoriteFruit":"apple"},{"id":31,"isActive":true,"balance":"3,460.04","age":30,"username":"Abigail","email":"abigailmckee@zensus.com","phone":"+1(984)496-3669","registered":"2020-08-25T01:52:26-08:00","latitude":87.002621,"longitude":-38.911094,"tags":["amet","pariatur","proident","cillum","in","duis","qui"],"greeting":"Hello,undefined!Youhave6unreadmessages.","favoriteFruit":"strawberry"},{"id":32,"isActive":false,"balance":"3,720.77","age":31,"username":"Ruiz","email":"ruizmckee@zensus.com","phone":"+1(848)469-2279","registered":"2015-10-24T06:17:33-08:00","latitude":-58.896833,"longitude":7.025146,"tags":["ullamco","occaecat","nisi","adipisicing","veniam","reprehenderit","irure"],"greeting":"Hello,undefined!Youhave4unreadmessages.","favoriteFruit":"strawberry"},{"id":33,"isActive":true,"balance":"3,014.05","age":29,"username":"Collier","email":"colliermckee@zensus.com","phone":"+1(875)557-3031","registered":"2020-08-27T12:18:45-08:00","latitude":64.591316,"longitude":-87.169913,"tags":["sit","nulla","sint","id","duis","reprehenderit","adipisicing"],"greeting":"Hello,undefined!Youhave1unreadmessages.","favoriteFruit":"banana"},{"id":34,"isActive":true,"balance":"2,028.29","age":38,"username":"Sadie","email":"sadiemckee@zensus.com","phone":"+1(910)554-2893","registered":"2019-03-18T05:00:57-08:00","latitude":-24.263531,"longitude":14.855673,"tags":["officia","cillum","est","duis","velit","quis","enim"],"greeting":"Hello,undefined!Youhave8unreadmessages.","favoriteFruit":"apple"},{"id":35,"isActive":true,"balance":"1,561.30","age":20,"username":"Chapman","email":"chapmanmckee@zensus.com","phone":"+1(908)414-2543","registered":"2020-10-16T03:10:41-08:00","latitude":8.034721,"longitude":-145.131804,"tags":["dolor","anim","proident","tempor","consequat","ipsum","velit"],"greeting":"Hello,undefined!Youhave5unreadmessages.","favoriteFruit":"apple"},{"id":36,"isActive":true,"balance":"3,536.22","age":27,"username":"Galloway","email":"gallowaymckee@zensus.com","phone":"+1(970)446-2556","registered":"2015-10-18T04:18:11-08:00","latitude":-66.944486,"longitude":-143.806012,"tags":["quis","proident","reprehenderit","veniam","anim","voluptate","enim"],"greeting":"Hello,undefined!Youhave2unreadmessages.","favoriteFruit":"apple"},{"id":37,"isActive":true,"balance":"2,080.36","age":28,"username":"Dianna","email":"diannamckee@zensus.com","phone":"+1(983)400-2468","registered":"2016-03-18T10:45:04-08:00","latitude":-83.729513,"longitude":-110.771729,"tags":["anim","ea","nostrud","dolor","magna","consectetur","tempor"],"greeting":"Hello,undefined!Youhave9unreadmessages.","favoriteFruit":"strawberry"},{"id":38,"isActive":false,"balance":"1,484.28","age":25,"username":"Elsie","email":"elsiemckee@zensus.com","phone":"+1(870)515-3100","registered":"2018-04-24T05:09:23-08:00","latitude":48.739835,"longitude":96.256471,"tags":["dolore","magna","irure","quis","consequat","eu","adipisicing"],"greeting":"Hello,undefined!Youhave5unreadmessages.","favoriteFruit":"banana"},{"id":39,"isActive":true,"balance":"2,272.51","age":27,"username":"Grace","email":"gracemckee@zensus.com","phone":"+1(847)448-2234","registered":"2017-04-25T03:11:43-08:00","latitude":28.372655,"longitude":-17.323308,"tags":["culpa","elit","magna","elit","nostrud","tempor","dolore"],"greeting":"Hello,undefined!Youhave8unreadmessages.","favoriteFruit":"apple"},{"id":40,"isActive":false,"balance":"3,128.01","age":27,"username":"Scott","email":"scottmckee@zensus.com","phone":"+1(954)569-2317","registered":"2016-11-25T06:56:54-08:00","latitude":23.128915,"longitude":98.232304,"tags":["commodo","non","consectetur","laboris","cupidatat","Lorem","do"],"greeting":"Hello,undefined!Youhave6unreadmessages.","favoriteFruit":"apple"},{"id":41,"isActive":true,"balance":"3,720.90","age":38,"username":"Melba","email":"melbamckee@zensus.com","phone":"+1(830)404-3765","registered":"2016-10-30T02:19:12-08:00","latitude":84.728934,"longitude":120.650407,"tags":["laborum","aliqua","minim","cillum","aliqua","enim","incididunt"],"greeting":"Hello,undefined!Youhave1unreadmessages.","favoriteFruit":"apple"},{"id":42,"isActive":false,"balance":"3,041.59","age":37,"username":"Berta","email":"bertamckee@zensus.com","phone":"+1(995)548-2412","registered":"2015-05-15T04:39:48-08:00","latitude":76.51089,"longitude":-36.770177,"tags":["deserunt","minim","qui","aliquip","quis","eu","eiusmod"],"greeting":"Hello,undefined!Youhave8unreadmessages.","favoriteFruit":"banana"},{"id":43,"isActive":true,"balance":"2,008.12","age":24,"username":"Anita","email":"anitamckee@zensus.com","phone":"+1(821)488-2329","registered":"2022-02-16T09:33:46-08:00","latitude":61.611504,"longitude":-43.435384,"tags":["nulla","ut","enim","amet","adipisicing","veniam","amet"],"greeting":"Hello,undefined!Youhave10unreadmessages.","favoriteFruit":"apple"},{"id":44,"isActive":false,"balance":"1,404.88","age":30,"username":"Louisa","email":"louisamckee@zensus.com","phone":"+1(943)516-2188","registered":"2016-08-10T11:20:23-08:00","latitude":-70.276405,"longitude":58.914492,"tags":["irure","nostrud","velit","enim","labore","id","ullamco"],"greeting":"Hello,undefined!Youhave5unreadmessages.","favoriteFruit":"strawberry"},{"id":45,"isActive":false,"balance":"1,503.90","age":37,"username":"Marie","email":"mariemckee@zensus.com","phone":"+1(973)568-3017","registered":"2019-08-19T08:05:57-08:00","latitude":71.916984,"longitude":-158.612553,"tags":["aliquip","cupidatat","sit","amet","nisi","nulla","elit"],"greeting":"Hello,undefined!Youhave8unreadmessages."},{"id":46,"isActive":false,"balance":"2,347.77","age":38,"username":"Janelle","email":"janellemckee@zensus.com","phone":"+1(940)501-3123","registered":"2017-04-19T12:45:18-08:00","latitude":-71.843323,"longitude":141.156843,"tags":["Lorem","reprehenderit","mollit","pariatur","in","dolore","culpa"],"greeting":"Hello,undefined!Youhave3unreadmessages.","favoriteFruit":"strawberry"},{"id":47,"isActive":false,"balance":"3,982.52","age":36,"username":"Cheri","email":"cherimckee@zensus.com","phone":"+1(877)402-2657","registered":"2021-11-16T09:03:57-08:00","latitude":-12.500234,"longitude":-136.904004,"tags":["Lorem","velit","cillum","ex","anim","pariatur","non"],"greeting":"Hello,undefined!Youhave2unreadmessages.","favoriteFruit":"strawberry"},{"id":48,"isActive":true,"balance":"1,892.86","age":40,"username":"Bush","email":"bushmckee@zensus.com","phone":"+1(855)444-2550","registered":"2017-05-03T10:21:37-08:00","latitude":-2.348724,"longitude":119.2084,"tags":["non","tempor","ipsum","eu","amet","aute","enim"],"greeting":"Hello,undefined!Youhave6unreadmessages.","favoriteFruit":"apple"},{"id":49,"isActive":true,"balance":"3,300.77","age":28,"username":"Burch","email":"burchmckee@zensus.com","phone":"+1(937)511-3492","registered":"2021-01-30T11:41:31-08:00","latitude":-48.961518,"longitude":-15.346772,"tags":["excepteur","proident","ipsum","esse","do","sit","commodo"],"greeting":"Hello,undefined!Youhave6unreadmessages.","favoriteFruit":"apple"},{"id":50,"isActive":false,"balance":"1,368.95","age":26,"username":"Booker","email":"bookermckee@zensus.com","phone":"+1(987)587-2545","registered":"2015-05-20T01:31:32-08:00","latitude":61.984045,"longitude":-125.380179,"tags":["laboris","esse","minim","et","magna","ex","proident"],"greeting":"Hello,undefined!Youhave9unreadmessages.","favoriteFruit":"banana"},{"id":51,"isActive":false,"balance":"1,901.40","age":36,"username":"Faye","email":"fayemckee@zensus.com","phone":"+1(946)600-2847","registered":"2016-02-03T01:15:43-08:00","latitude":-28.08036,"longitude":-7.373616,"tags":["nostrud","excepteur","laborum","elit","sint","id","commodo"],"greeting":"Hello,undefined!Youhave4unreadmessages.","favoriteFruit":"strawberry"},{"id":52,"isActive":false,"balance":"1,665.98","age":40,"username":"Johnnie","email":"johnniemckee@zensus.com","phone":"+1(856)591-3673","registered":"2021-05-19T05:08:44-08:00","latitude":-30.056231,"longitude":-19.230976,"tags":["dolore","duis","minim","irure","exercitation","sint","sunt"],"greeting":"Hello,undefined!Youhave3unreadmessages.","favoriteFruit":"strawberry"},{"id":53,"isActive":true,"balance":"1,474.40","age":31,"username":"April","email":"aprilmckee@zensus.com","phone":"+1(845)409-2098","registered":"2021-03-22T07:35:53-08:00","latitude":-5.786634,"longitude":56.888648,"tags":["dolore","culpa","deserunt","in","ea","id","ut"],"greeting":"Hello,undefined!Youhave1unreadmessages.","favoriteFruit":"apple"},{"id":54,"isActive":true,"balance":"3,300.08","age":35,"username":"Kathleen","email":"kathleenmckee@zensus.com","phone":"+1(871)562-3959","registered":"2021-12-12T05:47:47-08:00","latitude":-34.926525,"longitude":102.345966,"tags":["incididunt","sit","ea","qui","dolor","do","qui"],"greeting":"Hello,undefined!Youhave4unreadmessages.","favoriteFruit":"apple"},{"id":55,"isActive":true,"balance":"3,503.40","age":21,"username":"Hammond","email":"hammondmckee@zensus.com","phone":"+1(982)589-3035","registered":"2021-07-29T07:32:51-08:00","latitude":72.984266,"longitude":35.085423,"tags":["in","sit","nostrud","culpa","et","exercitation","cupidatat"],"greeting":"Hello,undefined!Youhave10unreadmessages.","favoriteFruit":"banana"},{"id":56,"isActive":false,"balance":"2,149.11","age":26,"username":"Chavez","email":"chavezmckee@zensus.com","phone":"+1(933)418-2082","registered":"2014-05-13T07:38:35-08:00","latitude":74.535419,"longitude":-124.452737,"tags":["amet","officia","velit","tempor","sunt","exercitation","adipisicing"],"greeting":"Hello,undefined!Youhave5unreadmessages.","favoriteFruit":"strawberry"},{"id":57,"isActive":true,"balance":"1,181.71","age":36,"username":"Carlson","email":"carlsonmckee@zensus.com","phone":"+1(828)407-3719","registered":"2015-10-29T04:23:45-08:00","latitude":-32.909352,"longitude":177.408092,"tags":["laborum","dolore","excepteur","culpa","labore","ullamco","do"],"greeting":"Hello,undefined!Youhave2unreadmessages.","favoriteFruit":"apple"},{"id":58,"isActive":false,"balance":"2,467.18","age":22,"username":"Morgan","email":"morganmckee@zensus.com","phone":"+1(839)513-2398","registered":"2020-09-01T08:25:27-08:00","latitude":27.835247,"longitude":-30.500342,"tags":"dolore,fugiat,anim","greeting":"Hello,undefined!Youhave10unreadmessages.","favoriteFruit":"banana"},{"id":59,"isActive":false,"balance":"2,692.63","age":36,"username":"Lynda","email":"lyndamckee@zensus.com","phone":"+1(974)484-3374","registered":"2018-09-24T01:15:56-08:00","latitude":-4.651066,"longitude":-88.589914,"tags":["adipisicing","duis","dolore","velit","commodo","tempor","pariatur"],"greeting":"Hello,undefined!Youhave3unreadmessages.","favoriteFruit":"strawberry"},{"id":60,"isActive":true,"balance":"3,681.55","age":37,"username":"Maryanne","email":"maryannemckee@zensus.com","phone":"+1(957)568-3099","registered":"2017-05-21T04:14:46-08:00","latitude":46.012669,"longitude":-159.55826,"tags":["officia","laborum","in","incididunt","minim","adipisicing","minim"],"greeting":"Hello,undefined!Youhave3unreadmessages.","favoriteFruit":"apple"},{"id":61,"isActive":true,"balance":"2,925.17","age":23,"username":"Richardson","email":"richardsonmckee@zensus.com","phone":"+1(934)507-2009","registered":"2015-12-11T09:08:36-08:00","latitude":-76.771339,"longitude":-156.898958,"tags":["reprehenderit","consectetur","aliquip","consectetur","officia","non","id"],"greeting":"Hello,undefined!Youhave8unreadmessages.","favoriteFruit":"apple"},{"id":62,"isActive":true,"balance":"1,661.74","age":30,"username":"Ronda","email":"rondamckee@zensus.com","phone":"+1(854)499-2748","registered":"2019-02-23T08:42:11-08:00","latitude":-17.063641,"longitude":-38.553417,"tags":["officia","irure","mollit","adipisicing","irure","dolor","cupidatat"],"greeting":"Hello,undefined!Youhave2unreadmessages.","favoriteFruit":"banana"},{"id":63,"isActive":true,"balance":"2,595.95","age":29,"username":"Michelle","email":"michellemckee@zensus.com","phone":"+1(815)576-3766","registered":"2019-11-09T08:32:18-08:00","latitude":14.176912,"longitude":164.448364,"tags":["esse","non","non","sint","do","amet","laboris"],"greeting":"Hello,undefined!Youhave4unreadmessages.","favoriteFruit":"strawberry"},{"id":64,"isActive":false,"balance":"3,491.92","age":38,"username":"Dolores","email":"doloresmckee@zensus.com","phone":"+1(906)543-2133","registered":"2022-01-25T05:16:21-08:00","latitude":-6.811998,"longitude":151.352407,"tags":["Lorem","culpa","est","deserunt","labore","voluptate","ea"],"greeting":"Hello,undefined!Youhave5unreadmessages.","favoriteFruit":"apple"},{"id":65,"isActive":true,"balance":"3,609.82","age":32,"username":"Christie","email":"christiemckee@zensus.com","phone":"+1(858)435-2179","registered":"2016-12-24T06:53:56-08:00","latitude":-61.337755,"longitude":-161.172187,"tags":["nostrud","incididunt","eu","fugiat","esse","magna","nostrud"],"greeting":"Hello,undefined!Youhave2unreadmessages.","favoriteFruit":"banana"},{"id":66,"isActive":true,"balance":"1,983.97","age":31,"username":"Lindsay","email":"lindsaymckee@zensus.com","phone":"+1(972)461-3979","registered":"2015-02-25T03:26:51-08:00","latitude":-31.182554,"longitude":-46.15873,"tags":["dolore","cillum","elit","occaecat","sint","Lorem","voluptate"],"greeting":"Hello,undefined!Youhave6unreadmessages.","favoriteFruit":"strawberry"},{"id":67,"isActive":false,"balance":"2,872.96","age":33,"username":"Olivia","email":"oliviamckee@zensus.com","phone":"+1(977)485-3523","registered":"2019-05-20T05:28:01-08:00","latitude":37.01348,"longitude":-20.686415,"tags":["ullamco","qui","nostrud","et","cillum","esse","et"],"greeting":"Hello,undefined!Youhave4unreadmessages.","favoriteFruit":"banana"},{"id":68,"isActive":false,"balance":"2,644.99","age":20,"username":"Hudson","email":"hudsonmckee@zensus.com","phone":"+1(977)525-3646","registered":"2014-09-17T06:12:40-08:00","latitude":-76.698465,"longitude":-53.394675,"tags":["pariatur","esse","qui","ullamco","fugiat","officia","pariatur"],"greeting":"Hello,undefined!Youhave3unreadmessages.","favoriteFruit":"banana"},{"id":69,"isActive":false,"balance":"3,132.67","age":31,"username":"Dotson","email":"dotsonmckee@zensus.com","phone":"+1(995)431-3943","registered":"2017-03-13T09:42:54-08:00","latitude":79.578958,"longitude":-113.18148,"tags":["et","consectetur","occaecat","anim","esse","ea","ipsum"],"greeting":"Hello,undefined!Youhave5unreadmessages.","favoriteFruit":"apple"},{"id":70,"isActive":"true","balance":"3,125.21","age":21,"username":"Floyd","email":"floydmckee@zensus.com","phone":"+1(849)430-3468","registered":"2021-06-01T12:55:05-08:00","latitude":-34.919145,"longitude":174.163382,"tags":["amet","consectetur","velit","et","dolor","dolor","est"],"greeting":"Hello,undefined!Youhave7unreadmessages.","favoriteFruit":"strawberry"},{"id":71,"isActive":true,"balance":"3,683.60","age":35,"username":"Beasley","email":"beasleymckee@zensus.com","phone":"+1(827)494-3977","registered":"2015-02-10T03:59:44-08:00","latitude":-73.593769,"longitude":143.92953,"tags":["anim","laboris","quis","labore","duis","magna","sit"],"greeting":"Hello,undefined!Youhave5unreadmessages.","favoriteFruit":"banana"},{"id":72,"isActive":false,"balance":"3,375.83","age":33,"username":"Roberta","email":"robertamckee@zensus.com","phone":"+1(810)577-3754","registered":"2016-03-17T05:24:50-08:00","latitude":-23.120394,"longitude":58.612489,"tags":["ut","eiusmod","consequat","laborum","aliquip","quis","amet"],"greeting":"Hello,undefined!Youhave6unreadmessages.","favoriteFruit":"strawberry"},{"id":73,"isActive":false,"balance":"3,045.69","age":32,"username":"Freeman","email":"freemanmckee@zensus.com","phone":"+1(823)503-3745","registered":"2019-08-12T05:46:30-08:00","latitude":-80.556985,"longitude":-104.552454,"tags":["consequat","deserunt","do","in","pariatur","est","officia"],"greeting":"Hello,undefined!Youhave10unreadmessages.","favoriteFruit":"strawberry"},{"id":74,"isActive":false,"balance":"1,717.98","age":20,"username":"Catalina","email":"catalinamckee@zensus.com","phone":"+1(942)443-2996","registered":"2015-04-16T10:18:41-08:00","latitude":-20.941469,"longitude":-17.722748,"tags":["et","sit","consectetur","laborum","deserunt","magna","amet"],"greeting":"Hello,undefined!Youhave3unreadmessages.","favoriteFruit":"strawberry"},{"id":75,"isActive":false,"balance":"1,274.47","age":33,"username":"Irwin","email":"irwinmckee@zensus.com","phone":"+1(946)521-3313","registered":"2019-09-07T12:48:45-08:00","latitude":-47.913944,"longitude":-40.715072,"tags":["esse","aliquip","sit","ex","occaecat","Lorem","consequat"],"greeting":"Hello,undefined!Youhave1unreadmessages.","favoriteFruit":"strawberry"}]
注意 读取文本文件时,Python将其中的所有文本都解读为字符串。如果你读取的是数字,并要将其作为数值使用,就必须使用函数int() 将其转换为整数,或使用
函数float() 将其转换为浮点数。
- 写入空文件
在调用open() 时需要提供另一个实参,第一个实参也是要打开的文件的名称;第二个实参(‘w’ )告诉Python,我们要以写入模式 打开这个文件。打开文件
时,可指定读取模式 (‘r’ )、写入模式 (‘w’ )、附加模式 (‘a’ )或让你能够读取和写入文件的模式(‘r+’ )。如果你省略了模式实参,Python将以默认的只读模式打
开文件。
如果你要写入的文件不存在,函数open() 将自动创建它。然而,以写入(‘w’ )模式打开文件时千万要小心,因为如果指定的文件已经存在,Python将在返回文件对象前清空
该文件。 #你要给文件添加内容,而不是覆盖原有的内容,可以附加模式 ‘a’ 打开文件
filename = ‘programming.txt’
with open(filename, ‘w’) as file_object:
file_object.write(“I love programming.”)
file_object.write(“I love creating new games.\n”) #写入多行,需要在write()语句中包含换行符
5. 分析文本
方法split() 以空格为分隔符将字符串分拆成多个部分,并将这些部分都存储到一个列表中。
- 存储数据(用模块json存储)
函数json.dump() 接受两个实参:要存储的数据以及可用于存储数据的文件对象。
import json #导入json模块
numbers = [2, 3, 5, 7, 11, 13]
filename = ‘numbers.json’ #们指定了要将该数字列表存储到其中的文件的名称。通常使用文件扩展名.json来指出文件存储的数据为JSON格式
with open(filename, ‘w’) as f_obj: #以写方式打开,让json将数据写入其中
json.dump(numbers, f_obj) #使用函数json.dump() 将数字列表存储到文件numbers.json中
json.load() 将这个列表读取到内存
import json
filename = ‘numbers.json’
with open(filename) as f_obj: #Python只需读取这个文件
numbers = json.load(f_obj) #使用函数json.load() 加载存储在numbers.json中的信息,并将其存储到变量numbers 中
print(numbers)
7. csv文件格式【将数据作为一系列以逗号分隔的值写入文件】
import csv #导入csv模块
filename = ‘文件名’
with open(filename) as f: #打开文件并将结果文件存入f中
reader = csv.reader(f) #调用csv.reader(),将前面存储的文件对象作为实参传递给它,从而创建一个与该文件相关联的阅读器(reader)对象。我们将这个阅读器对象存储在reader 中。
header_row = next(reader)#模块csv 包含函数next() ,调用它并将阅读器对象传递给它时,它将返回文件中的下一行,将返回的数据存储在header_row 中
print(header_row)