import pymongo client = pymongo.MongoClient('localhost',27017) #利用pymongo的mongoclinet 方法构造clinet xiaoshuo = client['xiaoshuo'] #利用clinet对象建立xiaoshuo表 sheet_1 = xiaoshuo['sheet_1'] #建立表内的不同sheet #读取小说信息 path = '/Users/JiaWeiFang/Downloads/117524.txt' with open (path,'r') as f: lines = f.readlines() for index,line in enumerate(lines): data = { 'index':index, 'line':line, 'words':len(line.split()) } sheet_1.insert_one(data) #利用sheet_1.insert_one()方法一个一个插入sheet表 这个动作不要重复做 #'$lt' '$lte' '$gt' '$gte' '$ne' #for item in sheet_1.find({'words':{'$lt':10}}): #通过利用sheet.find方法 读取想要的东西 在函数内使用字典删选 #print(item)